• 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: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do shift operators behave when the right hand operand is negative.
For e.g.
45 >> -6;
Thanks in advance
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
45>>-6
is the same as
45>> (32-6)
hope this helps!
correct me if wrong!
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When shifting a primitive int the shift distance is always between 0 and 31 inclusive. Just assume that the bit value of the shift distance is always "anded" with 0x1f. The binary value of -6 is
1111 1111 1111 1111 1111 1111 1111 1010
And the above with the 0x1f.
0000 0000 0000 0000 0000 0000 0001 1111
The result is 26.
0000 0000 0000 0000 0000 0000 0001 1010
I doubt that you will see a negative shift distance on the real exam, but Kathy or Bert would be a more authoritive source of information concerning what might actually appear.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Bit Shifters -
'At this point', I don't think you'd see a negative shifting question on the exam, but the exam is not cast in stone. I think this is a valid discussion because it 'might' come up in an exam revision.
There are certain topics that seem way out of scope, based on knowing some of the exam maker's philosophies, but I could imagine this one sneaking in.
An example of the type of question that I don't think is likely to sneak in, is one where a detailed knowledge of precedence is required... the test makers all seem to be pretty strong advocates of parentheses.
CAUTION: All of this is speculation on my part - maybe safe bets, but definitely NOT anything I'd bet my life on. (Although I'll take $5 bets ).
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
comment >>
I've felt bit shifting and bit cast representation is kind of important subject while one is dealing with network socket programming. CRC-check and encrption & decrption parts is all about bit shifiting.
That's why sun ask about the question. right?
I think kathy & berts book describes really well on this subject for the scjp2 test. i am enjoying the book a lot.
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic