Negative number right shift operator in C++ - c++

I'm new to C++ and I find something I can't understand. Could anyone provide some help?
For the following codes:
int i = -3;
printf("i=%d\n",i);
i = i >> 1:
printf("i >> 1 evaluates to: %d\n", i);
then I got the result:
i=-3
i >> 1 evaluates to: -2
I don't quite understand.
As 3 is coded as( let is be simple):
3 : 0000 0011
-3 : 1111 1100
then after right shift operation, we should have:
-1 : 1111 1110
right? Why I got -2? (My PC in 64 bit)
Thanks for any help!

Your mistake is in assuming that because 3 is 00000011, -3 is represented simply by inverting bits (the so-called "one's complement" representation of negative numbers) to get 11111100. And that likewise 00000001 becomes 11111110 when negated. In fact that's not the case—instead your computer seems to be using the almost-universal "two's complement" system in which -3 is represented as 11111101, -2 is 11111110 and -1 is 11111111.
One nice intuition pump for the two's-complement system is to consider a series of increments, and to note that the behavior is somewhat consistent and intuitive regardless of whether you imagine them happening in the bit pattern itself, in the signed representation, or in the unsigned. Let's stick to 8 bits for simplicity (imagine the "9th bit" just getting discarded):
bit pattern interpreted as...
signed byte unsigned byte
11111101 -3 253
11111110 -2 254
11111111 -1 255
00000000 0 0 (wrap-around)
00000001 1 1
When it goes from -1 to 0 I can almost "hear" all those bits flipping over one after the other.

Actually -1 = 0xFFFF = 1111 1111 1111 1111b, -3 = 0xFFFD = 1111 1111 1111 1101b(for 4 byte int).
So when you use right shift, you get 1111 1111 1111 1110b which is -2

Related

Two's Complement on representing negative numbers

I am currently trying to gain a more intuitive understanding of two's complement and its uses; however, I cannot seem to perform subtraction using two's complement correctly. I understand that when a negative number is stored in a signed int variable the procedure is to perform two's complement on the number having the MSB be 1 to represent the negative sign.
So in a 4 bit system 1010 represents -6.
Now I am following this guide on how to subtract two binary #s
For example I have the number 0101 (5 in decimal) and 1010 (-6 in decimal). If I wanted to do the equation 5 - (-6) it would look like 0101 - 1010 in binary. Next I would take the 1010 and perform twos' complement on it to get 0110. Now I take 0101 + 0110 and get 1011. I don't have a carry so I perform two's complement on the result giving me 0101, but this says the answer is -5 when it should be 11.
4 bits can represent 16 different values. With two's complement, they are -8 to 7.
1000 -8
1001 -7
1010 -6
1011 -5
1100 -4
1101 -3
1110 -2
1111 -1
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
You can't possibly get an answer of 11. It's too large to fit in 4 bits as a two's complement number. As such, the calculation you are attempting results in an overflow. (See below for how to detect overflows.)
This means 4 bits is not enough to compute 5 - -6. You need at least 5 bits.
0(...0)0101 5
- 1(...1)1010 -6
------------------
0(...0)0101 5
+ 0(...0)0110 6
------------------
0(...0)1011 11
Detecting Overflow in Two's Complement
With two's complement numbers, an addition overflows when the carry into the sign bit is different than the carry out of it.
0101 5
+ 0110 6
-----------
1011 -5 Carry in: 1 Carry out: 0 OVERFLOW!
1011 -5
+ 1010 -6
-----------
0101 5 Carry in: 0 Carry out: 1 OVERFLOW!
0001 1
+ 0010 2
-----------
0011 3 Carry in: 0 Carry out: 0 No overflow
1111 -1
+ 1110 -2
-----------
1101 -3 Carry in: 1 Carry out: 1 No overflow
For example I have the number 0101 (5 in decimal) and 1010 (-6 in decimal). If I wanted to do the equation 5 - (-6) it would look like 0101 - 1010 in binary.
Ok.
Next I would take the 1010 and perform twos' complement on it to get 0110.
Ok. And 0110 binary is 6 decimal is -(-6) decimal.
Now I take 0101 + 0110 and get 1011.
Ok.
I don't have a carry so I perform two's complement on the result giving me 0101,
I take you to mean that as part of the process of interpreting the result, not computing it. The result itself is 1011 (4-bit, two's complement binary).
but this says the answer is -5 when it should be 11.
The answer in the operational system you have chosen, with 4-bit two's complement, is -5, exactly as you have computed. With three data bits and one sign bit, the maximum result your data type can represent is 7 (0111 binary). This underscores the importance of choosing data types appropriate for the computations you want to perform.
Note that 0b1011 is 11 in binary, if interpreted as unsigned or with more than 3 data bits, so both 11 and -5 are the result (which you use depends on how its interpreted).
In terms of modular arithmetic, 4 bits is a base of 24, and:
11 ≡ -5 (mod 16)
Which answer you use depends on whether the nibble is interpreted as signed or unsigned.

C++ left shift operation

Here is my code
int a=2147483647;
int b= a<<1;
cout<<"a="<<a<<", b="<<b;
The output I am getting is-
a=214783647, b=-2
Binary representation of a is
0111 1111 1111 1111 1111 1111 1111 1111
By shifting it by 1 bit, it will change sign bit and replace LSB with 0. So, I think answer will be -ve and magnitude will be subtracted by 1 i.e
-2147483646
But it is giving result as -2 . Please explain.
This is because your computer is using 2 complement for the signed value.
Unsigned shifted value is 0xFFFFFFFE, which is -2 in 2 complement, not -2147483647.
Shifting is implementation defined in C.
BTW, -2147483647 is 0x80000001 on such CPU.
[expr.shift]/1 The value of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are zero-filled. ... if E1 has a signed type and non-negative value, and E1 × 2^E2 is representable in the corresponding unsigned type of the result type, then that value, converted to the result type, is the resulting value; otherwise, the behavior is undefined.
Emphasis mine. Your program exhibits undefined behavior.
Edit: Upon closer consideration, I no longer think it's undefined behavior. 2147483647*2 does fit into unsigned int, "the corresponding unsigned type" of int. Its conversion to int is not undefined, but merely implementation-defined. It's entirely reasonable for an implementation using two's complement to define this conversion so that 2147483647*2 == -2, just reinterpreting the bit pattern, as other answers explained.
Well, there is a very long story behind.
Since int is a signed type, it means that the first bit is a sign and the whole system is two-complement.
so x = 0b 1111 1111 1111 1111 1111 1111 1111 0111 is x = -9
and for example x = 0b 1111 1111 1111 1111 1111 1111 1111 1111 is x = -1 and x = 0b 0000 0000 0000 0000 0000 0000 0000 0010 is 2
Learn more about Two complement.

How does 1 left shift by 31 (1 << 31) work to get maximum int value? Here are my thoughts and some explanations I found online

I'm fairly new to bit manipulation and I'm trying to figure out how (1 << 31) - 1 works.
First I know that 1 << 31 is
1000000000000000000000000000
and I know it's actually complement of minimum int value, but when I tried to figure out (1 << 31) - 1, I found an explanation states that, it's just
10000000000000000000000000000000 - 1 = 01111111111111111111111111111111
I was almost tempted to believe it since it's really straightforward. But is this what really happening? If it's not, why it happens to be right?
My original thought was that, the real process should be: the two's complement of -1 is
11111111111111111111111111111111
then (1 << 31) - 1 =
(1)01111111111111111111111111111111
the leftmost 1 is abandoned, then we have maximum value of int.
I'm really confused about which one is right.
It's both! 1 << 31 is:
1000 0000 0000 0000 0000 0000 0000 0000
Subtracting 1 gives:
0111 1111 1111 1111 1111 1111 1111 1111
One of the nice features about the two's complement layout of signed numbers is that addition and subtraction are exactly the same operations as they are for unsigned numbers. So 10000...000 represents a negative number in two's complement, the largest negative number, which is -2,147,483,648 in this case, and subtracting 1 from it causes wrap-around to the largest positive number, 2,147,483,647, but two's complement numbers are arranged so that we can pretend it's an unsigned number instead, so the subtraction is uncomplicated. Subtracting 1 from 10000...000 simply drops the leading 1 to a 0, and borrows a bunch of 1s, same as in decimal you get a bunch of 9s: 10000 - 1 = 9999.
It's also true that mathematically, (a - b) is the same as (a + (-b)), so we can do (1 << 31) + (-1) instead:
1000 0000 0000 0000 0000 0000 0000 0000 (1 << 31)
1111 1111 1111 1111 1111 1111 1111 1111 (-1)
-----------------------------------------
1 0111 1111 1111 1111 1111 1111 1111 1111 +
0111 1111 1111 1111 1111 1111 1111 1111 (truncate)
A 1 is carried out of the high end, and lost once the result is truncated back into a 32-bit integer.
Either way, that pattern, with a single 0 at the high end, then filled by 1s, is the representation of the maximum positive value for a two's complement integer of any width.
There are other ways to generate that pattern if you prefer, such as ~(1 << 31), and (-1 >>> 1) (where >>> means logical shift right) which is agnostic of the width of the integer.

2s Complement of a negative zero

I have problem: you know the 2s Complement so you can get the negative number of a positive one with the reverse and adding a one. e.g.
8 Bit
121 = 0111 1001
1st= 1000 0110
+ 0000 0001
---------
1000 0111 --> -121
So now if we have a -0
a zero looks as 8 bit
0000 0000
so a minus 0 should look
1111 1111 + 0000 0001
= 10000 0000
but that is 512
so I think that I've misunderstood something
To expand my previous comment to the question
1111 1111 + 0000 0001 in 8 bit is 0000 0000, the ninth bit is lost because there is no place from it.
And, yes the complement of a negative is a positive
-121 = 1000 0111
1st = 0111 1000
+ 0000 0001
---------
0111 1001 --> 121
Think of them as a circle, at one point there is 0, adding 1 at a time you go up to the opposite point (128 in 8 bit) at that point the sign is switched and the absolute value begin to decrease, e.g.: 128 + 1 = -127, as you continue to add 1 the value go back to 0 and the circle is completed.
So given a number of bit, you only have that much bit, no more, and if you want the value to be signed you really have only x-1 bit for the value, as the most significant bit is used for the sign (0 -> +; 1 -> -)
1 0000 0000b is 256, not 512. Truncated to 8 bits, it's 0.
This is because with two's complement, zero is zero. There is no positive or negative zero.
Compare this to one's complement or sign bit, where positive zero and negative zero are different values.

Bit manipulation (clear n bits)

Going through the book "Cracking the coding interview" by Gayle Laakmann McDowell, in bit manipulation chapter, it posts a question:
Find the value of (assuming numbers are represented by 4 bits):
1011 & (~0 << 2)
Now, ~0 = 1 and shifting it two times towards the left yields 100 ( = 0100 to complete the 4 bits). Anding 1011 with 0100 equals 0000.
However, the answer i have is 1000.
~0 is not 1 but 1111 (or 0xf). The ~ operator is a bitwise NOT operator, and not a logical one (which would be !).
So, when shifted by 2 places to the left, the last four bits are 1100. And 1100 & 1011 is exaclty 1000.
~0 does not equal 1. The 0 will default to being an integer, and the NOT operation will reverse ALL the bits, not just the first.
~ is the Bitwise Complement Operator.
The value of ~0 should be 1111 in 4 bits .
1011 & (~0 << 2)
= 1011 & ( 1111 << 2)
= 1011 & 1100
= 1000
1011 & (~0 << 2)
~0 is not 1 but rather 11112 or 0xF16.
Shifting 1111 to the left twice gives 1100 (the two leftmost bits have been dropped and filled in with 0s from the right).
Adding 1011 & 1100 gives 1 in each bit position for which the corresponding bit position is 1, otherwise 0. This follows that the result is 1000.