• 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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
plzz try this,
(Q1)
5<<32 results 5
5<<-32 results 5
5<<33 results 10
these are ok but
plzz explain this,
5 << -33 results -2147483648 i.e the lowest boundry of int datatype.
(Q2)
plzz explain
-1 >> 1 results -1 not 0
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nadeem,
Only 5 bits of the right operand are used the reason is that an int is a 32-bit value and can only be shifted 0 through 31 times.
Now see 5 << -33
-33 divided by 32 gives -1 (i assume you know the reason)
-1 in binary is 111111111111111111111111111111111
take only 5 bits it's now 11111 and equals 31 in decimal
given question becomes 5<<31 I think you know the rest.
second question is -1>>1 and sure it'll give -1 the reason is right shift is a signed operator i.e it preserves the sign.
the bits that fill in the shifted left bits have the value of the leftmost bit(before the shift operation).
 
Nadeem Malik
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Nasir Khan,
i am now satisfied.
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
i am looking for source of binary arithmetics conception and examples
higly appreciate any help in advance
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just do a search for binary addition or subtraction on one of the search engines. (Yahoo, Altavista, etc)
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some info for shift/bitwise operations on my page.
Check the tips page at
http://www.geocities.com/velmurugan_p
Hope this helps.
------------------
Velmurugan Periasamy http://www.geocities.com/velmurugan_p/
 
Do not threaten THIS beaver! Not even with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic