• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Shift operators on negative numbers ?

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! i want to clarify that how right shift and left shift operators process the negative numbers.
eg. -65 >> 2 = -17 For this first get binary of 65 i.e 1000001 then apply >> 2 it'll be 0010000 now add 1 in this; Ans will be -17 thats correct. But when I apply the same process on -12 >> 2 and many other numbers then i dont get exact answer. (12'binary 1100 apply >> 2 its 0011 then add 1 Now Answer will be -4 whenever it should be -3).

Can anyone of you please solve this problem.
 
author
Posts: 23928
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

-65 >> 2 = -17 For this first get binary of 65 i.e 1000001 then apply >> 2 it'll be 0010000 now add 1 in this; Ans will be -17 thats correct.



Just because your technique happens to yield the correct answer, doesn't mean the technique is correct.

To do bit shifting on negative numbers, you need to understand two complement notation. First, you need to know the bit pattern for -65. This is best achieved by taking a twos complement of 65.

Once you have -65, then shift as usual. Your result should be a negative number. To help figure out what it is, take a twos complement again to find the positive equivalent.

Henry
 
Henry Wong
author
Posts: 23928
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

But when I apply the same process on -12 >> 2 and many other numbers then i dont get exact answer. (12'binary 1100 apply >> 2 its 0011 then add 1 Now Answer will be -4 whenever it should be -3).



12 = 0000 1100
negate 12 = 1111 0011
two comp 12 = 1111 0100 = -12

-12 >> 2 = 1111 1101 = result
negate result = 0000 0010
two comp result = 0000 0011 = 3

hence result = -3

Henry
 
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And remember sports fans - this topic is for the 1.4 exam only - you 5.0 types can skip this entire sordid mess
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic