# Bitwise AND expanation

This topic is 2131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Could someone expain me the bitwise and operation (&) in C++?

For example:
What happens here?
 if(63 & 1) { ...... }  Edited by bigdilliams

##### Share on other sites
The bitwise AND operator applies the AND operation to each bit of the operands individually.

The AND operations does the following: If both operands are 1, the result is 1, in any other case it's 0.

##### Share on other sites
Wikipedias explanation is quite good:
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[/quote]

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

##### Share on other sites
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?

##### Share on other sites

Yes, but I made a small programm wich results in:
 int Zahl = (54 &amp; 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

##### Share on other sites
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?

##### Share on other sites
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.

[table]
[th='3']P AND (&amp;) Q[/th]
[tr][td]P[/td][td]Q[/td][td]P AND Q[/td][/tr]
[tr][td]0[/td][td]0[/td][td]0[/td][/tr]
[tr][td]0[/td][td]1[/td][td]0[/td][/tr]
[tr][td]1[/td][td]0[/td][td]0[/td][/tr]
[tr][td]1[/td][td]1[/td][td]1[/td][/tr]
[/table]

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 &amp;-operator in C++ on the values 0xF8 and 0x15 you would get 0x10 as a result (0xF8 &amp; 0x15 == 0x10)

EDIT:

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

##### Share on other sites

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.

##### Share on other sites

What if I make this:
 if(63 &amp; 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-statement work.

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

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
18
5. 5

• 9
• 9
• 33
• 13
• 13
• ### Forum Statistics

• Total Topics
632586
• Total Posts
3007222

×