Sign in to follow this  
bigdilliams

Bitwise AND expanation

Recommended Posts

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

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

Share this post


Link to post
Share on other sites
nife87    520
Wikipedias explanation is quite good:
http://en.wikipedia.org/wiki/Bitwise_operation#AND

[quote]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 this post


Link to post
Share on other sites
nife87    520
[quote name='bigdilliams' timestamp='1353248315' post='5002038']
Yes, but I made a small programm wich results in:
[CODE]
int Zahl = (54 & 63);
std::cout << Zahl;
[/CODE]
63= 111111
54= 110110
so Zahl = 54
What is the sense behind that?
[/quote]

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 this post


Link to post
Share on other sites
Radikalizm    4807
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][b]P[/b][/td][td][b]Q[/b][/td][td][b]P AND Q[/b][/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:

[b]0xF8:[/b] 1 1 1 1 1 0 0 0
[b]0x15:[/b] 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 :D Edited by Radikalizm

Share this post


Link to post
Share on other sites
Mussi    4407
[quote name='bigdilliams' timestamp='1353248721' post='5002041']
When will the if-loop start?
Is the condition everytime true, and is it possible to be false with an other number?
[/quote]

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 this post


Link to post
Share on other sites
kuramayoko10    390
[quote name='bigdilliams' timestamp='1353248721' post='5002041']
What if I make this:
[CODE]
if(63 &amp; 1)
{
...
}
[/CODE]

When will the if-loop start?
Is the condition everytime true, and is it possible to be false with an other number?
[/quote]

Looks like this question is more related with how the if-[b]statement[/b] 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

Share this post


Link to post
Share on other sites
ultramailman    1720
[quote name='bigdilliams' timestamp='1353248721' post='5002041']
What if I make this:
[CODE]
if(63 & 1)
{
...
}
[/CODE]

When will the if-loop start?
Is the condition everytime true, and is it possible to be false with an other number?
[/quote]

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[sub]10[/sub] = 00111111[sub]2[/sub]
1[sub]10[/sub] = 00000001[sub]2[/sub]

00111111
[u]00000001[/u] &
00000001

00000001[sub]2[/sub] = 1[sub]10[/sub]
so
[CODE]if(63 & 1)[/CODE]
is equal to
[CODE]if(1)[/CODE]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this