Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Shift.....Problem

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Why does this code, gives odd output: -2147483648
int j = 10<<30;
System.out.println(j);
Decimal - 10 in binary format is 00000000000000000000000000001010
and left shift,fills zero from the right.In this case,30 bits from the right will be zero.
Getting confused.....
Can someone please clarify.
Thanks.
Mukti
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
10 << 30 equals 1010000000000000000000000000000000
Which is negative because of the 1 on the far left side.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mukti,
-10 is actually represented as 1111....11110110. If this is unclear to you, you might want to review 2's complement representation.
Hope this helps.
 
Glade Wishart
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mukti,
I am sorry. I should probably explain this so that it is more clear. The Shift Left operator as you mention shifts all bits in a left n times, filling with 0 from the right.
A 10 decimal = 1010 in binary. If you shift all bits to the left while filling with 0 from the right you come out with 1010000000000000000000000000000000 which equals -2147483648.
Does this make sense to you?
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glade,
Your explanation was fine except the shifted binary form.
It should be
1000000000000000000000000000000000000
i.e -2147483648
[This message has been edited by venu gopal (edited January 22, 2001).]
 
This is my favorite show. And this is my favorite tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic