17.09.2018, 17:51
Quote:
|
Hi, I need help with bit operations..
Lets say I have all bits "turned on" Код:
11111111111111111111111111111111 Код:
new myVar = 8; Код:
11111110111111111111111111111111 Код:
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
|
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
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


