Bit operation
#5

Quote:
Originally Posted by RakeDW
Посмотреть сообщение
Hi, I need help with bit operations..
Lets say I have all bits "turned on"

Код:
11111111111111111111111111111111
Now I have variables ex.

Код:
new myVar = 8;
And I must turn off 8th bit and get something like

Код:
11111110111111111111111111111111
I tried

Код:
new data; // all bits off
new myVar1 = 2, myVar2 = 8, myVar3 = 12;
data |= myVar1; // turn on 2nd bit
data |= myVar1; // turn on 12th bit

print((data & myVar1) ? ("myVar1 passed") : ("myVar1 failed"));
print((data & myVar2) ? ("myVar2 passed") : ("myVar2 failed"));
print((data & myVar3) ? ("myVar3 passed") : ("myVar3 failed"));

// Out: 
[20:49:25] myVar1 passed
[20:49:25] myVar2 failed
[20:49:25] myVar3 failed
First off, 8 fails because you didn't set it.
Furthermore you use myVar1 twice and expect it to do different things (but in fact you set the 2nd twice).

Your test should be:

Код:
data |= myVar1; // turn on 2nd bit
data |= myVar2; // turn on 8th bit
data |= myVar3; // turn on 12th bit
But even that isn't correct as Dayrion said. Your test was just wrong additionally to the operations.

Also 8 wouldn't set the 8th bit. 8 in binary is not equal to b10000000. 8 is b1000.
12 is also two bits that get set. It's b1100
Reply


Messages In This Thread
Bit operation - by RakeDW - 15.09.2018, 18:50
Re: Bit operation - by RakeDW - 17.09.2018, 15:24
Re: Bit operation - by Grim_ - 17.09.2018, 17:44
Re: Bit operation - by Dayrion - 17.09.2018, 17:51
Re: Bit operation - by NaS - 17.09.2018, 17:51
Re: Bit operation - by RakeDW - 17.09.2018, 21:12

Forum Jump:


Users browsing this thread: 1 Guest(s)