• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 23928
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: 23928
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.
reply
    Bookmark Topic Watch Topic
  • New Topic