1

I am asking this question as a follow up to my previous question.

The page at W3chools says:

The examples above uses 4 bits unsigned binary numbers. Because of this ~ 5 returns 10.

Since JavaScript uses 32 bits signed integers, it will not return 10.

It will return -6. 00000000000000000000000000000101 (5)

11111111111111111111111111111010 (~5 = -6)

A signed integer uses the leftmost bit as the minus sign.

Inverting the bits and chopping off the sign bit, the number becomes 1111111111111111111111111111010, which evaluates to 2147483642 in decimal form. According to the page, however, it should evaluate to -6.

Where did I go wrong?

2

5 Answers 5

3

The mistake lies in "chopping off the sign bit" of the 32 bit complemented result. This converts -6 (which has lots of leading 1s in it's binary representation) into a 31 bit positive number which still has lots of leading 1s in its binary representation. The 31 bit number obtained by removing the sign bit from -6 is very close to the maximum positive value of a 32 bit signed number.

You can see the results without removing the 32 bit integer sign bit:

function unsignedNibble( i) {
    return i & 0x0f;
}
function signedNumberBits( n) {
    var bits = "";
    for( var i = 32; i--;) {
        bits = "" + (n&1) + bits;
        n = n >> 1;
    }
    return bits;
}
console.log("Unsigned 4 bit: ");
var un5  = unsignedNibble( 5); // unsigned 4 bits of 5
console.log( "un5 = %s ( 0b%s)", un5, un5.toString(2));

var notUn5 = unsignedNibble( ~un5);
console.log( "~un5 = %s ( 0b%s)", notUn5, notUn5.toString(2));

console.log("Signed 32 bit: ");
var sn5 = 5; // signed number 5
console.log( "sn5 = %s ( 0b%s)", sn5, sn5.toString(2));
var notSn5 = ~sn5;
console.log( "~sn5 = %s ( 0b%s)", notSn5, signedNumberBits(notSn5));

1

@wais

6 is represent as 0000 0110 (0 has been padded)

now to get -6 we have to make 2's Complement for that

Step 1 - Reverse all the bits (1st Complement)

1111 1001

Step 2 - Add 1 (0000 0001)

1111 1001 + 0000 0001 = 1111 1010

0

In 2's compliment, you add 1 at the end after the bits are all complimented to get the negative number's absolute value.

1111111111111111111111111111010 is complimented to get ...0101 (which is 5), but then you add 1 to get 6. Remembering this was a negative number you're determining, you slap the negative sign on to get -6.

0

JavaScript binary numbers are stored in two's complement format.

This means that a negative number is the bitwise NOT of the number plus 1. For example:

Binary Representation               Decimal value
00000000000000000000000000000101      5
11111111111111111111111111111011     -5
00000000000000000000000000000110      6
11111111111111111111111111111010     -6
00000000000000000000000000101000     40
11111111111111111111111111011000    -40

Source: https://www.w3schools.com/js/js_bitwise.asp

-1

~ operator just do bit change.

such as ~5 = ~(00000101) it returns (11111010) = -6

Don't make ~ operator too complicated.

1
  • 1
    How is 11111010 equal to -6? This does not answer the question.
    – Wais Kamal
    Jun 11, 2018 at 22:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.