top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Bitwise Shift and Negative Numbers in C/C++

+2 votes
1,402 views

Bitwise operation is not completed until unless we discuss Negative numbers behavior in C. A negative number is the one which has highest bit set to 1.

Now coming to bitshift operation of left shift or right shift. When we leftshift any number irrespective of the sign the least significant bits are filled with zeros. Which makes it the case of of perfect multiplication of 2 on each shift.

Example

[1111 1111 1111 1111] << 4

After First  Shift : [1111 1111 1111 1110] (-1*2 = -2)
After Second Shift : [1111 1111 1111 1100] (-2*2 = -4) 
After Third  Shift : [1111 1111 1111 1000] (-4*2 = -8) 
After Fourth Shift : [1111 1111 1111 0000] (-8*2 = -16) 

When we right shift the any number using the right shift operator then the most significant bit is filled from the right side i.e. if number is negative then 1 is filled else 0 is filled. Which makes it the perfect case of division by 2 in each right shift. Another important point here in case of a positive number, say 5, division by 2 gives 2.5 rounding off to the nearest smallest integer i.e. 2 but when we consider a negative number, -5, division by 2 gives -2.5. Its rounding off to the nearest integer gives -3. In case of overflow you always get -1.

Example

[1111 1111 1111 0000] >> 4

After First  Shift : [1111 1111 1111 1000] (-16/2 = -8)
After Second Shift : [1111 1111 1111 1100] (-8/2 = -4) 
After Third  Shift : [1111 1111 1111 1110] (-4/2 = -2) 
After Fourth Shift : [1111 1111 1111 1111] (-2/2 = -1)

Now see at this combined example for more clarity

main()
{
 int a=15, b=-15;
 printf("%d\n", a << 4); // 15*2*2*2*2 = 240
 printf("%d\n", a >> 1); // 15/2 = 7
 printf("%d\n", b << 4); // -15*2*2*2*2 = -240
 printf("%d\n", b >> 1); // -15/2 = -8
 printf("%d", b >> 5);   // -15/(2*2*2*2*2) = -1 i.e. overflow
} 
posted May 25, 2014 by Salil Agrawal

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Masking is a process of forcing a particulate bit of a variable as OFF or ON based on the need. Using the masking we retain required data and rest of the data is blocked. Popular way of of masking is use of | (OR) or & (AND) operator.

Use of AND (&) operator for masking
Suppose we have a variable with the value as "01010101" and we want to suppress the higher 4 bits and retain lower 4 bits, it can be achieved in the following way.

Value x = 0101 0101
Mask  y = 0000 1111
Result z = x & y i.e. 0000 0101

Use of OR (I) operator for masking
Suppose we have a variable with the value as "01010101" and we want to set the higher 4 bits as 1 and retain lower 4 bits as it is, it can be achieved in the following way.

Value x = 0101 0101
Mask  y = 1111 0000
Result z = x | y i.e. 1111 0101 
READ MORE

C language supports the following bit-wise operators.

| – Bitwise OR
& – Bitwise AND
~ – One’s complement
^ – Bitwise XOR
<< – left shift
>> – right shift

The | (OR) operator compares two values, and returns a value that has its bits set if one or the other values, or both, have their corresponding bits set. The bits are compared using the following table

1   |   1   ==   1
1   |   0   ==   1
0   |   1   ==   1
0   |   0   ==   0

The & (AND) operator compares two values, and returns a value that has its bits set if, and only if, the two values being compared both have their corresponding bits set. The bits are compared using the following table

1   &   1   ==   1
1   &   0   ==   0
0   &   1   ==   0
0   &   0   ==   0

The ~ (Ones Complement or inversion) operator acts only on one value and it inverts it, turning all the ones int zeros, and all the zeros into ones.

The ^ (XOR) operator compares two values, and returns a value that has its bits set if one or the other value has its corresponding bits set, but not both. The bits are compared using the following table

1   ^   1   ==   0
1   ^   0   ==   1
0   ^   1   ==   1
0   ^   0   ==   0

The >> (Right shift) and << (left shift) operators move the bits the number of bit positions specified. The >> operator shifts the bits from the high bit to the low bit. The << operator shifts the bits from the low bit to the high bit.

Example

#include <stdio.h>

main()
{
   unsigned int a = 60; /* 60 = 0011 1100 */  
   unsigned int b = 13; /* 13 = 0000 1101 */
   int c;           

   c = a & b;       /* 12 = 0000 1100 */ 
   printf("Line 1 - Value of c is %d\n", c );

   c = a | b;       /* 61 = 0011 1101 */
   printf("Line 2 - Value of c is %d\n", c );

   c = a ^ b;       /* 49 = 0011 0001 */
   printf("Line 3 - Value of c is %d\n", c );

   c = ~a;          /*-61 = 1100 0011 */
   printf("Line 4 - Value of c is %d\n", c );

   c = a << 2;     /* 240 = 1111 0000 */
   printf("Line 5 - Value of c is %d\n", c );

   c = a >> 2;     /* 15 = 0000 1111 */
   printf("Line 6 - Value of c is %d\n", c );
}
READ MORE
...