• 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

Please Help with 1's / 2's complements

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have read many places like
i = 7;
Print ~i .
I don't know how to find out this. On this forum , I have read few answers that explain with " ...... .. first convert a bit pattern of like 0000 1101 to 1's complement or to 2's " complement .
I don't know how do we get 1's or 2's complements .
Can somebody help me out how do we get complements.
In the discussion of >>> / << / >> operators for negative numbers also I find these technical words .
Please explain.
Thanks in advance.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by JM:
I have read many places like
i = 7;
Print ~i .
I don't know how to find out this. On this forum , I have read few answers that explain with " ...... .. first convert a bit pattern of like 0000 1101 to 1's complement or to 2's " complement .
I don't know how do we get 1's or 2's complements .
Can somebody help me out how do we get complements.
In the discussion of >>> / << / >> operators for negative numbers also I find these technical words .
Please explain.
Thanks in advance.


Since nobody took a shot at this, I will try
Negative numbers are represented in computers using the 2's complement format. To get the 2's complement of a given number
1. Write the number in binary.
2. Take the 1's complement of it. 1's complement is nothing but flipping all 0s to 1s and all 1s to 0s.
3. Then add 1 to that. Ignore any overflow
For example: To write the number 11 in 2's complement, first write the number in binary
1. To convert to binary, continue to divide by 2, until you get a final remainder of 0 or 1. Take the remainders in the reverse order. 11 is written as 0000 1011.
2. Taking 1's complement would give 1111 0100.
3. Adding 1 would give 1111 0101
So, the 2's complement of 11 is 11110101.
Hope this helps.
Savithri
PS: Use all 32 bits if you are operating on an integer.
[This message has been edited by Savithri Devaraj (edited July 10, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot . It was getting on my nerves. I was not able to get the questions on shift operators right. Hope this will help.
Once again thanks.
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

JM/Savithri:
FYI.....another related discussion
Regds.
- satya
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi all,
I will be giving the exam this week.The >> and >>> operations are still confusing me particularly with respect to the sign.Could somebody give me an explaination or suggest a site i could refer to?
Thanks,
Regards
Aparanji
 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The operator >> means "shift right with sign extension".
The operator >>> means "shift right with zero fill."
For example,
public static void main(String args[])
{
int number = -128;
int anotherNumber = -128;
System.out.println(number >> 1); // prints -64
System.out.println(anotherNumber >>> 1); // prints 2147483584
}
As you can see, using >> fills the leftmost bits with the same
sign as the original number, whereas using >>> fills the
leftmost bits with 0's.
Hope this helps.
 
reply
    Bookmark Topic Watch Topic
  • New Topic