• 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

shifting with negative numbers

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls explain the execution of the question given below....


code
--------------------------------------

public class Test{
public static void main(String args[]){
int a = -8;
int b = -33;
a>>>=b;
System.out.println(a);
}}
-----------------------------------------

How do we evaluate the problem when the shift distance is a negative number?
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right shift - number * 2^ whatever position (raise to)
Left shift - number / 2 ^ whatever position (raise to)

unsigned right shift - always positive
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry my mistake - vice versa
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Previous discussion of this exact topic in this forum...

https://coderanch.com/t/263391/java-programmer-SCJP/certification/Shift-operators-negative-numbers

Henry
 
ram shah
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
henry,
in the rarlier discussion only the left hand operand was negative. here, both operands are negative. I've doubt regarding this situation only. How do we right shift or left shift a number(positive or negative) by a larger negative number?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ram shah:
henry,
in the rarlier discussion only the left hand operand was negative. here, both operands are negative. I've doubt regarding this situation only. How do we right shift or left shift a number(positive or negative) by a larger negative number?



For shifting of ints only the lower 5 bits of right operand is significant (for longs, it is the lower 6 bits). Everything else from the previous discussion remains the same.

8 = 0000 0000 0000 0000 0000 0000 0000 1000
negate 8 = 1111 1111 1111 1111 1111 1111 1111 0111
twos comp 8 = 1111 1111 1111 1111 1111 1111 1111 1000 = -8

33 = 0000 0000 0000 0000 0000 0000 0010 0001
negate 33 = 1111 1111 1111 1111 1111 1111 1101 1110
twos comp 33 = 1111 1111 1111 1111 1111 1111 1101 1111 = -33
low 5 bits of -33 = 0000 0000 0000 0000 0000 0000 0001 1111 = 31

-8 = 1111 1111 1111 1111 1111 1111 1111 1000
-8 >>> 31 = 0000 0000 0000 0000 0000 0000 0000 0001 = 1

Henry
 
ram shah
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry....I got it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic