Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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

question on bit shifting

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
Hey,
Why does the following code print the value of i as 1 and not 0:
int i=1;
i = i >> 32;
System.out.println(i);
What happens to i when it goes past 0 in leftshift,
Also, I have seen in one of the examples that i<<32 will print the result as 1, and i<<33 will result in value 2.
I know that 1<<31 will result in i being set to a negative value which is the maximum value for that type. But what happens after that.
Please clarify!!
Bye,
Tualha Khan
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shifting operations never shift more than 31 bits(in case of int) or 63 bits(in case of long). If the RHS operand is more than 31/63, then it is reduced to RHS % 32/64.
So in the following case, no shifting happens since 32 % 32 is zero,so the value still is 1 after the shift.
int i = 1;
i = i >> 32;
In case of i << 33, what really happens is i << 1, so if the value of i was 1 before the shifting, it becomes 2 after the one bit left shifting.
HTH
------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear Tualha Khan,
remember %32 whenever you are doing bitwise shifting, take mod 32
of R.H.S operand.
ex :
1 >> 4 // here calculating 4 % 32 = 4 thus 4 bit shifting .
1 >> 31 // here calculating 31 % 32 = 31 thus 31bit shifting .
1 >> 32 // here calculating 32 % 32 = 0 thus 0 bit shifting .
1 >> 33 // here calculating 33 % 32 = 1 thus 1 bit shifting .
during bitwise shifting before any shifting could takes place
the compiler do %32 on RHS operand, which by default is int.
In case it is long than %64 . . .
all the best with bits,
vikas singh .
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic