Diference between 0 and (1 << 0)?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
weeix
Posts: 23
Joined: Fri Jul 09, 2010 5:07 am

Diference between 0 and (1 << 0)?

Post 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?)
weeix
Posts: 23
Joined: Fri Jul 09, 2010 5:07 am

Post 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
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

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

Post 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.
Last edited by greenya on Wed Aug 11, 2010 7:10 am, edited 1 time in total.
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post 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
weeix
Posts: 23
Joined: Fri Jul 09, 2010 5:07 am

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

Post 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?
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post 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.
weeix
Posts: 23
Joined: Fri Jul 09, 2010 5:07 am

Post 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
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post 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.
slavik262
Posts: 753
Joined: Sun Nov 22, 2009 9:25 pm
Location: Wisconsin, USA

Post 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.
weeix
Posts: 23
Joined: Fri Jul 09, 2010 5:07 am

Post by weeix »

BTW, thanks everyone for answering my question!
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post 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.
slavik262
Posts: 753
Joined: Sun Nov 22, 2009 9:25 pm
Location: Wisconsin, USA

Post by slavik262 »

I was just referencing the fact that C had it first, but yeah, it is a pretty generic programming feature.
terier
Posts: 34
Joined: Sun Oct 05, 2008 4:46 pm

Post by terier »

Well then here's another one for you to figure out:
Why did they invent the so-called bit fields?
slavik262
Posts: 753
Joined: Sun Nov 22, 2009 9:25 pm
Location: Wisconsin, USA

Post 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.
Mikhail9
Posts: 54
Joined: Mon Jun 29, 2009 8:41 am

Post 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.
Post Reply