Page 1 of 2

Diference between 0 and (1 << 0)?

Posted: Wed Aug 11, 2010 6:08 am
by weeix
From the Collision Tutorial
// I use this ISceneNode ID to indicate a scene node that is
// not pickable by getSceneNodeAndCollisionPointFromRay()
ID_IsNotPickable = 0,

// I use this flag in ISceneNode IDs to indicate that the
// scene node can be picked by ray selection.
IDFlag_IsPickable = 1 << 0,
What's the difference between 0 and (1 << 0)?

(I read from MSDN Document that the value of a left-shift expression x << y is x * 2y.
So 1 << 0 is 1 * 2(0) = 1 * 0 = 0. Did I misunderstand?)

Posted: Wed Aug 11, 2010 6:50 am
by weeix
I didn't understand this one either,
node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/dwarf.x"),
0, IDFlag_IsPickable | IDFlag_IsHighlightable);
IDFlag_IsPickable | IDFlag_IsHighlightable is (1 << 0) | (1 << 1) = 0 | 2 = 2?

0 0 0 0 OR
0 0 1 0
--------
0 0 1 0

Re: Diference between 0 and (1 << 0)?

Posted: Wed Aug 11, 2010 7:08 am
by greenya
weeix wrote: What's the difference between 0 and (1 << 0)?
The difference is that 0 == 0 , and 1 << 0 == 1. In tutorial it written in this way to show that enum can be extended and next values will be "1<<2", "1<<3", ...
weeix wrote: (I read from MSDN Document that the value of a left-shift expression x << y is x * 2y.
So 1 << 0 is 1 * 2(0) = 1 * 0 = 0. Did I misunderstand?)
The links show a power of y, not a miltiplication.
"x << y is x * 2y" <--is not the same as--> "x << y is x * 2^y" as we know 2^0 == 1.

Posted: Wed Aug 11, 2010 7:09 am
by greenya
weeix wrote:I didn't understand this one either,
node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/dwarf.x"),
0, IDFlag_IsPickable | IDFlag_IsHighlightable);
IDFlag_IsPickable | IDFlag_IsHighlightable is (1 << 0) | (1 << 1) = 0 | 2 = 2?

0 0 0 0 OR
0 0 1 0
--------
0 0 1 0
The result of "IDFlag_IsPickable | IDFlag_IsHighlightable" is:
0 0 0 1 // 1 == 1 << 0
0 0 1 0 // 2 == 1 << 1
--------
0 0 1 1 // 3

Re: Diference between 0 and (1 << 0)?

Posted: Wed Aug 11, 2010 7:42 am
by weeix
I don't understand why these shift operation are invented. Are they necessary?

Could I use:

Code: Select all

        ID_IsNotPickable = 0,
        IDFlag_IsPickable = 1,
        IDFlag_IsHighlightable = 2
instead?

Posted: Wed Aug 11, 2010 8:12 am
by greenya
Yes.

For first 3 members you can use just:
ID_IsNotPickable = 0,
IDFlag_IsPickable = 1,
IDFlag_IsHighlightable = 2

but if you need to add more, you have to use 4 (not 3), than 8 (not 4), than 16 (not 5) and so on.

Posted: Wed Aug 11, 2010 9:06 am
by weeix
1 2 4 8 16 32 64 ..

I see. It's easier to use x << y in this case.

But I still don't understand why can't I just use 1 2 3 4 5 6 .. happily? What is the purpose of this complication? O_o

Posted: Wed Aug 11, 2010 9:17 am
by greenya
We use 1,2,4,8,16,... (power of 2 values) when we want to pack into single integer value couple of independent triggers (true/false value or 1/0). When we do so, we simply use bit-logic operators to check values.

Posted: Wed Aug 11, 2010 12:07 pm
by slavik262
weeix wrote:What is the purpose of this complication? O_o
It's a carry-over from C. Since binary is base-2, the bit shift operator lets you access each individual bit easily. 1 << 0 is the first bit (in an 8-bit char, 0000 0001), and 1 << 5 (32) is the 5th bit (0001 0000). Using bit shift and binary logic commands such as &, |, and ^ (not && and ||), you can do things like use an int as a field of 32 booleans by controlling each bit.

Posted: Thu Aug 12, 2010 6:57 am
by weeix
BTW, thanks everyone for answering my question!

Posted: Thu Aug 12, 2010 9:49 am
by DtD
I wouldn't necessarily call it a carry over from C, it is computers in general. It is basically for accessing the bits inside the byte, so one byte can act like 8 bools. It is generally used for flexibility stuff.

Posted: Thu Aug 12, 2010 11:57 am
by slavik262
I was just referencing the fact that C had it first, but yeah, it is a pretty generic programming feature.

Posted: Thu Aug 12, 2010 11:31 pm
by terier
Well then here's another one for you to figure out:
Why did they invent the so-called bit fields?

Posted: Fri Aug 13, 2010 4:20 am
by slavik262
It makes things more convenient when you're doing storage, but there's still places where manual bit-shifting is needed. For example, in networking it's required to convert from host to network byte order and vice-versa (for little-endian machines). Now, this does usually come in a pre-made function (htons and ntohs, respectively), but I'm just trying to point out that it still has plenty of practical uses.

Posted: Fri Aug 13, 2010 4:42 am
by Mikhail9
terier wrote:Well then here's another one for you to figure out:
Why did they invent the so-called bit fields?
Fundamentally, they invented them because the hardware itself exposes a set of flip-flops (a hardware boolean) to the firmware, which are grouped together into sets of 4, 8, 16 and so on. These make the most sense to the software as nybbles, bytes, words, and so on, so that's how you work with them.

It's really a holdover from writing machine language to access hardware directly. You still do a lot of it in embedded programming, where setting the bits in a memory location directly affects the microcrontroller you're working with. I program my microcontrollers in C, so those bit-shift operators are my best friend.