• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Shift operators!

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I have the following:
int x=3;
x<<2
x>>2
x>>>2
I don't have any problem in using the shift operators for the above positive integer.If the integer is negative, I have problem in calculating the above values.Can anybody explain me with ease to understand thia.Thanks.
 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sridhar,
lets take an eg like this:
int i = -3;
i<<2;
i>>2;
i>>>2;
since all the negative values r represented in two's complement form,
binary represenattion of i = 0000 0000 0000 0000 0000 0000 0000 0011
binary represenattion of -3 is given in two's complement form as
-3 = 1111 1111 1111 1111 1111 1111 1111 1101( applying bitwise inversion and adding one to the obtained value)
now applying shift operators to i;
i<<2 - shifting left by two bits and appending 0's at the end.we get
i<<2 = 1111 1111 1111 1111 1111 1111 1111 0100
sice the result obtained is a negative no, agan it has to be represented in two's complement form.so, we will get
i<<2 = 0000 0000 0000 0000 0000 0000 0000 1100 - this is equivalent to 12 and since it is a negative no the ans will be -12.
so, finally -3<<2 = -12;
coming to -3>>2 - shifting -3 by two bits and appending the sign bits at the Most significant bits we will get
-3 = 1111 1111 1111 1111 1111 1111 1111 1101

-3>>2 = 1111 1111 1111 1111 1111 1111 1111 1111 (sice -3 is negative, introducing 1's in most significant bits)
as already seen, the result is agn a negative value and so taking the two's complement form,we get
-3>>2 = 0000 0000 0000 0000 0000 0000 0000 0001
so, the result of -3>>2 is -1
now, -3>>>2 - applying unsigned rigth shift by two bits( introducing zeros at MSB)
-3 = 1111 1111 1111 1111 1111 1111 1111 1101

-3>>>2 = 0011 1111 1111 1111 1111 1111 1111 1111
here no need to take two's complemet of the result ,as the result is a positive no. so, we can directly take the decimal representation of the obtained binary result.
Hope this will help u.
vineela.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sridhar,

suppose we r trying to find out the following
--> -3 >> 1
--> -3 << 1
--> -3 >>> 1
Note all the numbers are taken as integers (32 bits) not bytes or shorts
First see how to obtain -3's binary representation:
-3 binary code is obtained by taking 2'complement of 3's binary code.
2's complement means inverting all bits and then adding 1
Binary representation of 3: 00000000000000000000000000000011
Inverting all the bits we get: 11111111111111111111111111111100
lets add 1 to it: +
00000000000000000000000000000001
---------------------------------
11111111111111111111111111111101
---------------------------------
So The binary form of -3 is :11111111111111111111111111111101

so now lets see -3 << 1:
It means left shift -3 by one bit.
ie 11111111111111111111111111111101 << 1 = 11111111111111111111111111111010
--------------------------------
Observe that in the above -3 representation, left most bit is discarded and at the right most a '0' is appended, to get the underlined representation.
So the answer is 11111111111111111111111111111010
clearly the first bit is 1, so its a negative no. In order to obtain its magnitude we have to take 2's complement of it.
So follows the above said steps
something like this...
11111111111111111111111111111010
After Inversion
00000000000000000000000000000101
Adding 1 + 00000000000000000000000000000001
--------------------------------
00000000000000000000000000000101 = 6 in decimal form
--------------------------------
Hence its clear that answer or result has magnitude 6 and since its a negative no. Its -6 the required answer
Hence -3<<1 = -6
Similarly u can calculate for -3>>1 and -3>>>1
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Varsha, Vineela!
Thanks a lot for the detailed explaination.I think that I am clear now.Anyway I shall try out some and let u know.I have one doubt.I read that u can do a multiplication or dvision to order to get right or left shifted values.
Like for left shift mutiple by powers of 2 and for right shift divide by powers of 2.Isn't this wasy to get the answers.Also in which circumstances do we need to go for this.can u please explain.Thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic