• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Bitwise AND expanation

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1bigdilliams  Members

Posted 18 November 2012 - 08:01 AM

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

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


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

### #2Mussi  GDNet+

Posted 18 November 2012 - 08:09 AM

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

### #3Rattenhirn  Members

Posted 18 November 2012 - 08:10 AM

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.

### #4nife87  Members

Posted 18 November 2012 - 08:17 AM

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

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.

### #5bigdilliams  Members

Posted 18 November 2012 - 08:18 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?

### #6nife87  Members

Posted 18 November 2012 - 08:20 AM

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, 18 November 2012 - 08:21 AM.

### #7bigdilliams  Members

Posted 18 November 2012 - 08:25 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?

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.

P AND (&amp;) Q
PQP AND Q
000
010
100
111

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, 18 November 2012 - 08:32 AM.

I gets all your texture budgets!

### #9Mussi  GDNet+

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.

### #10kuramayoko10  Members

Posted 18 November 2012 - 08:44 AM

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, 18 November 2012 - 08:46 AM.

Programming is an art. Game programming is a masterpiece!

### #11ultramailman  Prime Members

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:
6310 = 001111112
110 = 000000012

00111111
00000001 &
00000001

000000012 = 110
so
if(63 & 1)
is equal to
if(1)

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

### #12Khatharr  Members

Posted 19 November 2012 - 06:27 PM

(P and Q).

Upvoted for going full on geek mode.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.