Bitwise operators...?
by Joe Spataro · in Torque Game Engine · 11/13/2003 (11:22 am) · 4 replies
I was reading the bulletholes tutorial and the author writes:
...
if(hitObject->getType() & (InteriorObjectType|TerrainObjectType))
{
...
}
Then he explains what the code does which makes sense, however, being a complete beginner I wanted to understand how this works so I looked up this
enum SimObjectTypes {
DefaultObjectType = 0,
StaticObjectType = bit(0),
EnvironmentObjectType = bit(1),
TerrainObjectType = bit(2),
InteriorObjectType = bit(3),
...
}
My questions:
In the enum, is bit() a function call?
What does it do; does it shift bits?
Once the whole thing is evaluated, what actually determines if it is true or false?
Since I dont know what bit() does, I then assume that TerrainObjectType = 2 and InteriorObjectType = 3, which if OR'd together you get 011, so if I AND that with what getType() returns I get some value which could effect the outcome...basically since the end result can be different based on which things are OR'd together and then AND'ed together...what actually determines whether the if is true or false?
...
if(hitObject->getType() & (InteriorObjectType|TerrainObjectType))
{
...
}
Then he explains what the code does which makes sense, however, being a complete beginner I wanted to understand how this works so I looked up this
enum SimObjectTypes {
DefaultObjectType = 0,
StaticObjectType = bit(0),
EnvironmentObjectType = bit(1),
TerrainObjectType = bit(2),
InteriorObjectType = bit(3),
...
}
My questions:
In the enum, is bit() a function call?
What does it do; does it shift bits?
Once the whole thing is evaluated, what actually determines if it is true or false?
Since I dont know what bit() does, I then assume that TerrainObjectType = 2 and InteriorObjectType = 3, which if OR'd together you get 011, so if I AND that with what getType() returns I get some value which could effect the outcome...basically since the end result can be different based on which things are OR'd together and then AND'ed together...what actually determines whether the if is true or false?
Torque Owner Badguy
1.) when or'ing bits there can be No Overlap.
to truly understand this you need to have some level of binary skill.
for example:
0000 == 0
0001 == 1
0010 == 2
0100 == 4
now see.. there is no 3
and see what would happen if there is:
0011 == 3
this tell's me that both 2 and 1 have been stored.
totalling 3 but there is no 3 bit
if there was how to test for 1 and 2?
so if I could examine bit(..) (which is a Macro)
(which I cannot im at work)
you would prolly find that :
bit(0)
should give:
00000000
while bit(1)
should give:
00000001
and leaving us with bit(2)
should give:
00000010
and I would assume from here that bit(3)
should give:
00000100
which is actually 4 but it is the 3rd available bit.
(looking at your example i could be off by one in this but that dont matter you get the picture?)
here in this test:
if(hitObject->getType() & (InteriorObjectType|TerrainObjectType))
the two bit's or'd together produce one output.
that output anded with getType checks if there is any similarities in the bits.
if all the bits set from getType match any of the bits in the output you have a true case where you can find the bit's match.
heh Am I making sense here?
I hope so .. hope this helps a bit.