# Please Help with 1's / 2's complements

Anonymous

Ranch Hand

Posts: 18944

posted 15 years ago

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.

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.

Savithri Devaraj

Ranch Hand

Posts: 103

posted 15 years ago

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).]

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

Madhav Lakkapragada

Ranch Hand

Posts: 5040

posted 15 years ago

Take a Minute, Donate an Hour, Change a Life

http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81

Aparanji Raju

Ranch Hand

Posts: 65

Stephanie Grasson

Ranch Hand

Posts: 347

posted 15 years ago

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.

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.