• 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

10 >> -2

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does shifting with a negative number work?
10 >> -2
Thanks.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cathy
It's not as ominous as it looks

In evaluating 10 >> -2, we use the least significant 5 bits of the integer value -2, which is 11110 (= 30)
Therefore,

Cheers
Harwinder
[ November 15, 2003: Message edited by: Harwinder Bhatia ]
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harwinder,
Can you tell me why least significant "5" bits of the integer value -2 are used?
Thanks.
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cathy
Again, I had the same doubt a few days back:
I had posted the same question but didn't get a complete answer. I was stuck at the same point: If the left hand operand of a shift operator is an integer, why use the least significant 5 bits of the right hand operand or why mask the value by 0x1f (both mean the same).
See this original thread initiated by me last month:
https://coderanch.com/t/243572/java-programmer-SCJP/certification/Dan-Operators-Assignments
I think I understand it slight better now:

Remember that for shift operations (when the promoted type of the left hand operand is an integer) the compiler performs a mod32 conversion. That means for a << b, the shift value will be b%32.

Lets take couple of examples here of shifting by a value greater than 32:


Apply this to any other integer value (including -2) and you'll find that using the least significant 5 bits has the same effect as mod32.
I hope I didn't confuse you further
Cheers
Harwinder
[ November 15, 2003: Message edited by: Harwinder Bhatia ]
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can you tell me why least significant "5" bits of the integer value -2 are used?


5 bits can represent the values 0 to 31. Therefore, the shift distance actually used is always in the range 0 to 31.
The right most bit shifted left 31 bits will end up as the left most bit. The left most bit shifted right 31 bits will end up as the right most bit.
0000 0000 0000 0000 0000 0000 0000 0001 the number to be shifted
0000 0000 0000 0000 0000 0000 0000 0001 shifted 0
0000 0000 0000 0000 0000 0000 0000 0010 shifted 1
1000 0000 0000 0000 0000 0000 0000 0000 shifted 31
If you shifted any more than 31 bits, you would shift the whole number out of the 32-bit memory location.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My totally unsupported speculation is, the reason that only the 5 bits of the shift distance are used is a convenience for the programmer. We don't have to explicitly mask, it's done for us.
a << b
a << (b & 0x1f)
[ November 15, 2003: Message edited by: Marlene Miller ]
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More unsupported stuff:
To convert a negative int number N to the real bit representation do this: 2^32 - N
-N => 2^32 - N
Therefore,
-2 => 2^32 - 2 = 4294967296 - 2 = 4294967294
= 1111 1111 1111 1111 1111 1111 1111 1110
Check the output of:

as a testimony.
4294967294 % 32 = 30 = 11110 = (least significant 5 bits of 1111 1111 1111 1111 1111 1111 1111 1110)

"Hence Proved"
Cheers
Harwinder
[ November 16, 2003: Message edited by: Harwinder Bhatia ]
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it will go about in this manner.
-2 binary representation is
11111111 11111111 11111111 00000010
To find the value of the number take 2's complement.
(ie. take a compliment and add 1) which will result in
00000000 00000000 00000000 11111110
This is equvalent to the value 254.
The number of positions to be shifted = The value % 32
= 254%32
=30
So you have to right shift this
00000000 00000000 00000000 00001010
30 times.
You will get a 0 at the end of 3rd shift.
So Cathy, the expression 10>>-2 evaluates to zero.
If I am wrong in any place, please correct me.
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lakshmi Saradha:
I think it will go about in this manner.
-2 binary representation is
11111111 11111111 11111111 00000010
If I am wrong in any place, please correct me.


I'm afraid Lakshmi, this isn't correct. How did you derive "-2 = 11111111 11111111 11111111 00000010" ?
You could always verify this using the following:

It will give you an output:
1111 1111 1111 1111 1111 1111 1111 1110
Cheers
Harwinder
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Btw, this is what the documentation of Integer.toBinaryString says:


toBinaryString

public static String toBinaryString(int i)

Returns a string representation of the integer argument as an unsigned integer in base 2.

The unsigned integer value is the argument plus 2^32 if the argument is negative; otherwise it is equal to the argument. This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s.


So, all the good stuff which I thought was "unsupported" was infact "official"
Cheers
Harwinder
 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you hariwinder for pointing the wrong binary representation.
BTW, Any idea of whether we can take a calculator for the exam?
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lakshmi Saradha:
Thank you hariwinder for pointing the wrong binary representation.
BTW, Any idea of whether we can take a calculator for the exam?


You'll have to leave everything at the receptionist's desk. They'll give you a marker and a scratchboard (in my case).
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
6 bits for a long
(and I got a shovel blade and a piece of charcoal for my exam, plus it was snowing and uphill both ways)
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bert Bates:

(and I got a shovel blade and a piece of charcoal for my exam, plus it was snowing and uphill both ways)


Hopefully, they didn't order you to take off your coat.
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone..
 
Just the other day, I was thinking ... about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic