Title: Converting a Two's complement number to its binary representation
Tags: javascript;floating-point;binary;numbers;twos-complement
Question: I am performing bitwise operations, the result of which is apparently being stored as a two's complement number. When I hover over the variable it's stored in I see- num = -+1-478-300-1638.
The binary of that number that I want is - (10000011101000100001100000111000).
But when I say num.toString(2) I get a completely different binary representation, the raw number's binary instead of the 2s comp(-1111100010111011110011111001000).
How do I get the first string back?
Link to a converter: rapidtables.com/convert/number/decimal-to-binary.html
Put in this number: -+1-478-300-1638

Here is the accepted answer: pedro already answered this, but since this is a hack and not entirely intuitive I'll explain it.
I am performing bitwise operations, the result of which is apparently being stored as a two's complement number. When I hover over the variable its stored in I see num = -+1-478-300-1638
No, the result of most bit-operations is a 32bit signed integer. This means that the bit 0x80000000 is interpreted as a sign followed by 31 bits of value.
The weird bit-sequence is because of how JS stringifies the value, something like sign + Math.abs(value).toString(base);
How to deal with that? We need to tell JS to not interpret that bit as sign, but as part of the value. But how?
An easy to understand solution would be to add 0x100000000 to the negative numbers and therefore get their positive couterparts.
function print(value) {
if (value < 0) {
value += 0x100000000;
console.log(value.toString(2).padStart(32, 0));
print+1-478-300-1638);
Another way would be to convert the lower and the upper bits seperately
function print(value) {
var signBit = value < 0 ? "1" : "0";
var valueBits = (value &amp; 0x7FFFFFFF).toString(2);
console.log(signBit + valueBits.padStart(31, 0));
print+1-478-300-1638);
//or lower and upper half of the bits:
function print2(value) {
var upperHalf = (value &gt;&gt; 16 &amp; 0xFFFF).toString(2);
var lowerHalf = (value &amp; 0xFFFF).toString(2);
console.log(upperHalf.padStart(16, 0) + lowerHalf.padStart(16, 0));
print2+1-478-300-1638);
Another way involves the "hack" that pedro uses. You remember how I said that most bit-operations return an int32? There is one operation that actually returns an unsigned (32bit) interger, the so called Zero-fill right shift.
So number &gt;&gt;&gt; 0 does not change the bits of the number, but the first bit is no longer interpreted as sign.
function uint32(value){
return value&gt;&gt;&gt;0;
function print(value){
console.log(uint32(value).toString(2).padStart(32, 0));
print+1-478-300-1638);
will I run this shifting code only when the number is negative, or always?
generally speaking, there is no harm in running nr &gt;&gt;&gt; 0 over positive integers, but be careful not to overdo it.
Technically JS only supports Numbers, that are double values (64bit floating point values). Internally the engines also use int32 values; where possible. But no uint32 values. So when you convert your negative int32 into an uint32, the engine converts it to a double. And if you follow up with another bit operation, first thing it does is converting it back.
So it's fine to do this like when you need an actual uint32 value, like to print the bits here, but you should avoid this conversion between operations. Like "just to fix it".
Comment for this answer: should i choose yours or his as the answer? Clearly yours is more thorough.
Comment for this answer: In the second example, wouldn't the value in the parentheses when you are setting the valueBits potentially be negative, and thus yield 1-1(etc.) as you have said the Stringify will put a negative in front then display the rest of the bits as the value
Comment for this answer: I was worried that it would just interpret the first bit in 7FFFFFFF as a negative indicator, potentially, but then i realized it places the result of 0x7FFFFFFF into a 32 bit integer so your reply is quite right. Thank you.
Comment for this answer: The one that YOU feel that it best answers the question.
Comment for this answer: `(value & 0x7FFFFFFF)` can not be negative as it is excluding the bit that would contain the negative sign.

Here is another answer: Follow bellow the result:
var number = -+1-478-300-1638;
var bin = (number &gt;&gt;&gt; 0).toString(2)
//10000011101000100001100000111000
console.log(bin)
Comment for this answer: also, will I run this shifting code only when the number is negative, or always?
Comment for this answer: Could you explain your answer? Because shifting a number zero positions does not change it! Or does it? Or in other terms, what you're doing is a hack. Please explain the hack so OP and future readers also understand how/why this is working.
Comment for this answer: @a_here_and_now you can run it always, it's probably as quick if not qicker as the check wether the number is negative; and it does nothing to positive ints.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:10/15/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><floating-point><binary><numbers><twos-complement>', 'Title': "Converting a Two's complement number to its binary representation", 'ViewCount': 763, 'id': 58397356, 'user_AboutMe': '', 'user_AccountId': 11391835.0, 'user_DisplayName': 'a_here_and_now', 'user_Reputation': 177.0, 'user_Views': 274.0, 'user_id': 8351585.0}}