0

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

1
  • 2
    1000 == 0b1111101000 Commented Mar 24, 2016 at 20:14

3 Answers 3

4
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.

Sign up to request clarification or add additional context in comments.

Comments

2

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        

11 Comments

so is the 1000 is converted to 32 bit binary as stated here?
@Maximus all data in the computer stored as binary data, even video and music. And & just makes binary AND(multiplies bitwise) with numbers
@isvforall, yes, I know that, thank you :). My question was about specifics of type conversion in javascript
@NinaScholz, ok, thanks. Because you take 1000 as decimal - why do you say decimal?
@NinaScholz I feel deja vu :)
|
0

Yes, 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.

2 Comments

@georg op has "does the operator & perform the conversion to binary?" And I suppose I answered that. 2's complement format implies binary form
You (and the OP) seem to be confusing "binary notation" for numbers (like 0b1000) and their internal representation (which is always "binary" on a binary computer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.