Checking the BITS of an int(SOLVED)

Started by
18 comments, last by HyperHacker 18 years, 4 months ago
well it doesnt really matter which way it goes only importnat factor is which statement is true

if (step & 0x01) Print The number is Odd
or
if (step & 0x01) Print The number is > 128

Or in english does 0x01 check the bit under the ones column or under the largest column (128 in the case of a byte)
----------------------------

http://djoubert.co.uk
Advertisement
Quote:Original post by dawidjoubert
Quote:Original post by dbzprogrammer
Quote:Original post by dawidjoubert
Okay and is the bit order from left to right or right to left.

Oh and is this faster than a < or > check?

in the case of a byte is 1 the first bit or 128?


128 64 32 16 8 4 2 1


10000000 is 1
11000000 is 3
11100000 is 7

Left to right, right being the greatest, left being the least. What do you mean < or >?


if (step > 8) // Do stuff

However im assuming a > 8 check is simple a check bit no 4



I don't believe you can do that, and if you could, I think it would be if(step > 0x08). Perhaps if(step >> 0x08)?
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Quote:Would be more correct. Yes, that's how the logical order would go. Don't worry about other machienes right now.


Oops... yeah. Don't use the logical and.

I can see how confused you are, because I am right now also. I'm sorry for the mess up. 00000001 would be 1, 00000010 would be 2, etc... Logically this would be read: 00000001 from the right side to the left.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Quote:Original post by smitty1276
Quote:Would be more correct. Yes, that's how the logical order would go. Don't worry about other machienes right now.


Oops... yeah. Don't use the logical and.


Okay this whole thing is not working for me, i simple wanted to make a faster version of this

if (step > 8) step = 16;
else if (step > 4) step = 8;
else if (step > 2) step = 4;
else if (step > 1) step = 2;
else step = 1;

However replacing it with
if (step & 0x05) step = 16;
else if (step & 0x04) step = 8;
else if (step & 0x03) step = 4;
else if (step & 0x02) step = 2;
else step = 1;

Simple didnt work for me.

The thing is im doing that check about 16K times a frame and thought i could bring the FPS rate up by moving to a more effective method.

Perhaps at the end of the day ill end up doing it in ASM
----------------------------

http://djoubert.co.uk
Quote:Original post by dawidjoubert
Quote:Original post by smitty1276
Quote:Would be more correct. Yes, that's how the logical order would go. Don't worry about other machienes right now.


Oops... yeah. Don't use the logical and.


Okay this whole thing is not working for me, i simple wanted to make a faster version of this

if (step > 8) step = 16;
else if (step > 4) step = 8;
else if (step > 2) step = 4;
else if (step > 1) step = 2;
else step = 1;

However replacing it with
if (step & 0x05) step = 16;
else if (step & 0x04) step = 8;
else if (step & 0x03) step = 4;
else if (step & 0x02) step = 2;
else step = 1;

Simple didnt work for me.

The thing is im doing that check about 16K times a frame and thought i could bring the FPS rate up by moving to a more effective method.

Perhaps at the end of the day ill end up doing it in ASM


I don't know how the speed comparison would work out on that, sorry. and that wouldn't work, becaues 0x05 would be actually be 0x10.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Quote:Okay this whole thing is not working for me, i simple wanted to make a faster version of this

if (step > 8) step = 16;
else if (step > 4) step = 8;
else if (step > 2) step = 4;
else if (step > 1) step = 2;
else step = 1;


If your just wanting to check for a bit being set your doing it wrong with the "step & 0x03"... that is checking for 2 bits being checked. "step & (i < 2)" will check to see if the 3rd bit is checked.

This is the same thing... maybe this is what you meant. You need to make sure you are using powers of 2 (which is what the "1 << i" does).
if (step & 0x01) //first bit is set...else if (step & 0x02) //2nd bit is setelse if (step & 0x04) //3rd bit is set......etc...


It looks like you are going the other way.... the 0x05 at the top should be, as dbz said, 0x10 (or "(1 << 5)").
Furthermore, if you want an actual, functionally identical, alternative to this (with greater than sign):
if (step > 8) step = 16;else if (step > 4) step = 8;else if (step > 2) step = 4;else if (step > 1) step = 2;else step = 1;


You would need to make some small changes...

for (int i = 4; i >= 0; i++){   if (step > (1 << i))   {      step = (1 << (i+1));      break;   }   step = 1;}


At this point though, what's the point. It's probably going to compile to the same thing regardless.
I dunno whats the point really... Perhaps to test my own intelligence..which seem to have failed.

Anyways thanks guys i got it all sorted with the > checks.

So is there anyways to make a computer run infinitely fast without investing a cent? .... i didnt think so, bugger!
----------------------------

http://djoubert.co.uk
Just thought I might mention this:
Quote:Original post by dawidjoubertOh and is this faster than a < or > check?

Both of them would assemble down to one instruction, CMP or AND (IIRC). Generally, one instruction takes the same amount of time to execute (well, maybe a few hundred-billionths of a second difference) no matter which it is. (I think there are some which actually perform multiple instructions, but that's off topic.) For that matter your compiler or even CPU might optimize 'x > 8' into 'x & 8' or vice-versa.

Also, just to clarify what's already been said: Bits are numbered from right to left starting with 0. So 00000001 has bit 0 set, 00000010 has bit 1 set, and so on. The correct way to check a bit is to use the & (AND) operator to mask it out. For example (x & 4) checks bit 2, because the number 4 (00000100) only has bit 2 set. (x & 5) checks bits 2 and 0, since 5 (00000101) has bits 2 and 0 set.
---------------------------------dofile('sig.lua')

This topic is closed to new replies.

Advertisement