For example:

What happens here?

if(63 & 1) { ...... }

**Edited by bigdilliams, 18 November 2012 - 08:10 AM.**

11 replies to this topic

Sponsor:

Posted 18 November 2012 - 08:09 AM

Have you read http://en.wikipedia.org/wiki/Bitwise_operation? Anything in particular you don't understand?

Posted 18 November 2012 - 08:17 AM

Wikipedias explanation is quite good:

http://en.wikipedia.org/wiki/Bitwise_operation#AND

For instance, you have a = 100 (01100100) and b = 50 (00110010), and if you write c = a & b (100 & 50 == 32) the result is:

01100100 (100)

00110010 (50)

------------

00100000 (32)

Since the 6th column (from the right) is the only one with a 1 in both a and b.

EDIT: Way too slow, it seems :-)

http://en.wikipedia.org/wiki/Bitwise_operation#AND

A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. The result in each position is 1 if the first bit is 1 and the second bit is 1; otherwise, the result is 0. In this, we perform the multiplication of two bits; i.e., 1 × 0 = 0 and 1 × 1 = 1

For instance, you have a = 100 (01100100) and b = 50 (00110010), and if you write c = a & b (100 & 50 == 32) the result is:

01100100 (100)

00110010 (50)

------------

00100000 (32)

Since the 6th column (from the right) is the only one with a 1 in both a and b.

EDIT: Way too slow, it seems :-)

**Edited by nife87, 18 November 2012 - 08:19 AM.**

Posted 18 November 2012 - 08:20 AM

Yes, but I made a small programm wich results in:

int Zahl = (54 & 63); std::cout << Zahl;63= 111111

54= 110110

so Zahl = 54

What is the sense behind that?

Because if you multiply the numbers, bit for bit, and write it out (0x0=0, 1x0=0, 0x1=0, 1x1=1), that is what you get.

**Edited by nife87, 18 November 2012 - 08:21 AM.**

Posted 18 November 2012 - 08:30 AM

IMO the simplest way to explain the basic logical operators is by using truth tables. Let's have a look at the truth table for a logical AND with all possible input values for 2 random propositions (P and Q).

The leftmost column shows all values for P, the middle column all values for Q, and the rightmost column the results of the AND operation on the two inputs.

Now, how do we translate this operation to actual integer values which can range far beyond the values contained in a single bit (as portrayed in this truth table)?. It's quite easy actually, let's take a look at some 8-bit (1 byte) values:

We take 2 random values, let's say 0xF8 (248 decimal) and 0x15 (21 decimal). Let's lay these values out in binary form:

**0xF8:** 1 1 1 1 1 0 0 0

**0x15:** 0 0 0 1 0 1 0 1

When we look at these numbers in a binary form it gets much easier to do a logical AND operation on them. We iterate over the presented binary numbers in the first integer and we do a logical AND with the matching binary value in the other integer.

Let's have a look at the result:

1 1 1 1 1 0 0 0

0 0 0 1 0 1 0 1 (AND)

-------------------

0 0 0 1 0 0 0 0

If we put the resulting binary number back into a hexadecimal form we get 0x10 (16 decimal).

So when you use the &-operator in C++ on the values 0xF8 and 0x15 you would get 0x10 as a result (0xF8 & 0x15 == 0x10)

EDIT:

Wow, it seems like I take a very long time to post

The leftmost column shows all values for P, the middle column all values for Q, and the rightmost column the results of the AND operation on the two inputs.

P AND (&) Q | ||
---|---|---|

P | Q | P AND Q |

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

Now, how do we translate this operation to actual integer values which can range far beyond the values contained in a single bit (as portrayed in this truth table)?. It's quite easy actually, let's take a look at some 8-bit (1 byte) values:

We take 2 random values, let's say 0xF8 (248 decimal) and 0x15 (21 decimal). Let's lay these values out in binary form:

When we look at these numbers in a binary form it gets much easier to do a logical AND operation on them. We iterate over the presented binary numbers in the first integer and we do a logical AND with the matching binary value in the other integer.

Let's have a look at the result:

1 1 1 1 1 0 0 0

0 0 0 1 0 1 0 1 (AND)

-------------------

0 0 0 1 0 0 0 0

If we put the resulting binary number back into a hexadecimal form we get 0x10 (16 decimal).

So when you use the &-operator in C++ on the values 0xF8 and 0x15 you would get 0x10 as a result (0xF8 & 0x15 == 0x10)

EDIT:

Wow, it seems like I take a very long time to post

**Edited by Radikalizm, 18 November 2012 - 08:32 AM.**

Posted 18 November 2012 - 08:43 AM

When will the if-loop start?

Is the condition everytime true, and is it possible to be false with an other number?

In this case always, since the result > 0, 1 to be precise. It's possible to be false with other numbers, e.g. 2 & 1 = 0.

Posted 18 November 2012 - 08:44 AM

What if I make this:

if(63 & 1) { ... }

When will the if-loop start?

Is the condition everytime true, and is it possible to be false with an other number?

Looks like this question is more related with how the if-

The if-statement divides the flow of your program in 2 branches:

- Branch1 will be activated only if the value inside the if-statement evaluates to zero. This branch ignores the contents of the if-statement and starts immediately after its end or inside an else-statement associated with this if.

- Branch2 will be activated when the first one didn't. So, for every value different than zero, the if-statement will be executed.

**Edited by kuramayoko10, 18 November 2012 - 08:46 AM.**

Posted 18 November 2012 - 02:29 PM

What if I make this:

if(63 & 1) { ... }

When will the if-loop start?

Is the condition everytime true, and is it possible to be false with an other number?

You are mistaken. an "if" is not a loop, because it only runs once. If you want to loop, you have to use the while loop, for loop, or do while; loop. If you put plain numbers in the if brackets, the result will always be the same, either always true or always false.

In the case of 63 & 1:

63

1

00111111

00000001 &

00000001

00000001

so

if(63 & 1)is equal to

if(1)

That means what comes after the if will always be executed.