How come the following
0xf & 1000
evaluates to 8 ?
I'm specifying 1000 as non binary, does the operator & perform the conversion to binary? This also evaluates to 8
0xf & 0b1000
but the second number is explicitly defined as binary
How come the following
0xf & 1000
evaluates to 8 ?
I'm specifying 1000 as non binary, does the operator & perform the conversion to binary? This also evaluates to 8
0xf & 0b1000
but the second number is explicitly defined as binary
1111101000 // 1000
0000001111 // 0xf
---------- & operator
0000001000 // 8 in binary system
The numbers 1000 (dec) and 0b1000 (bin) have the same ending in binary representation (1000) and the same length as 0xf (four digits in binary - 1111)
Therefore you have the same result in both cases for bitwise operator.
Because you take 1000 as decimal. According to the documentation for Numbers, only numbers which starts with zero are treated as binary, octal, hexadecimal or depending as decimal.
f hex = 0000001111 bin
1000 dec = 1111101000 bin
& 0000001000 bin = 8 dec
& just makes binary AND(multiplies bitwise) with numbersYes, bitwise operators do that, see https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators for details.
The operands of all bitwise operators are converted to signed 32-bit integers in two's complement format.
0b1000) and their internal representation (which is always "binary" on a binary computer).