• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • 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: 23919
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: 23919
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.
 
Wanna see my flashlight? How about this tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic