java - Why does != 0 work but == 1 doesn't work when using bitwise &? -
private void displaybinary() { int value = 123; (int = 128; > 0; = / 2) { if ((value & i) != 0) system.out.print("1 "); else system.out.print("0 "); } }
the method above writes out 123 in binary form, program outputs 0111101. however, if change !=0 ==1 in if statement, outputs 0000001. since binary 0s , 1s, why doesn't latter work?
you're not working base-2 number; you're still working base-10 int
.
from this, numbers iteration, except last, fail if
condition, since not equal 1.
Comments
Post a Comment