• 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 , very urg !

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PLS
Caon someone explain me (step by step) a shift operation with < 0 ?

like: 235 << -8
Thx folks
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shift with a negative int does NOT reverse the sense of the shift. For maximum speed, the shift operator for int grabs the low five bits of the shift amount and ignores the sign bit and everything else.
Write yourself a test program. Use the handy methods in Integer that will write a number as a binary string.
System.out.println( Integer.toBinaryString( n )) ;
and you can watch the bits moving around.
Bill

------------------
author of:
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
235 << -8

In the case when u have -8 right side operand then u need to convert the no. into it's bit pattern.
The bit pattern of -8 in int form is ----
11111111 11111111 11111111 111111000
conside the 5 low order bits (in the case of int 5 & 6 low order bits in the case of long )which are 11000 .
Now convert this to a no. which would be 24 .
So the shift expression will change to 235 << 24.
I hope this clears the concept.
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic