• 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

shift

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here i don't understand for operator below.Please give me logic to calculate .Are there any formula for the below one .

-5 >> 2 = -2
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Negative numbers in bitwise arithmetic are confusing. You have to work out what the negative numbers look like in binary (two's complement representation). Then the bitwise operation will make sense.

When writing code, I would suggest that you never use negative integer literals (e.g. -2) in a bitwise expression. The clearest representation is usually hexadecimal literals (e.g. 0xFFFFFFFE), because that is very easily translatable to binary in the head of the person reading your code.
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The formula is to always convert the number to binary 0's and 1's, perform the operation, then convert it back. For exams or homework problems, you're not likely to find a better way to do it by hand.
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As previously mentioned, negative numbers and bitwise operations are tricky.
I'll use a hit (or nibble...4 bits) to show the steps:

0101 = 5
1010 = ~5 (1s complement - flip all the bits)
1011 = -5 (2s complement - add 1 to 1s complement)

now shift right by 2, carry the sign bit

1101 = shift one
1110 = shift two (result)

so the result is 1110, which is a negative number.

I'll apply 2s complement to translate to an easier to read positive number, keeping in mind that the answer is really negative.

1110 = result
0001 = ~result
0010 = -result = 2 (but we know that it's really negative so: ) -2
 
He does not suffer fools gladly. But this tiny ad does:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic