• 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: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,
i just completed my study for shift operators.i am confident in handling positive numbers,but am getting a lot wrong answers when i handle negative numbers.can the rule x/2^(number of shifts) for right shift and x*2^(number of shifts) be applied for -ve numbers also?will they test on any other primitive types other than int?

and also if we want to evaluate answers such as 2 power 31 when converting a binary form to decimal form, how is it possible to calculate in the exam?
please clasrify my doubt.
regards,
raja
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rajaraman ,
use this one concept for any question :

If the value to be shifted ( left side ) is an int then take last 5 digit of right hand operand for shifting . If long then take 6 digit .


let's apply this rule :
int i = 24;
i << -12

24 : 0000 0000 0000 0000 0000 0000 0001 1000


12 : 0000 0000 0000 0000 0000 0000 0000 1100

-12 : 1111 1111 1111 1111 1111 1111 1111 0100


taking last 5 digit : 10100 => 20

24 << 20

0000 0000 0000 0000 0000 0000 0001 1000 << 20

0000 0001 1000 0000 0000 0000 0000 0000

25165824

Hope it helps ...
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Formula for computing the number of shifts when the number of shifts given is negative:

for integer: (32*(x%32) + (32+x))
for long: (64*(x%64) + (64+x))
where x is a negative number that is in the range of int or long.(whichever is applicable)

Formula for computing the number of shifts when the number of shifts given is greater than the number of bits for the primitive type:

for integer: (x%32)
for long: (x%64)
where x is a positive number that is greater than the number of bits of int or long.(whichever is applicable)
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
x>>(a negative number) is x>>(32-negative number for int and 64-x for long)
The rest goes as usual...
For ex:
20>>-12is same as 20>>(32-12)
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

f we want to evaluate answers such as 2 power 31 when converting a binary form to decimal form, how is it possible to calculate in the exam?


It's very useful for programmers to memorize the powers of 2 up to 2^16. Beyond that, if you can recognize 2^31 and 2^63 when you see them in the SCJP, you're good to go.
 
What are you doing in my house? Get 'em tiny 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