# reverse operator

Anonymous

Ranch Hand

Posts: 18944

posted 15 years ago

- 0

What will be the output when you compile and execute the following program.

public class Base{

private void test() {

System.out.println(~6);

}

static public void main(String[] a) {

new Base().test();

}

}

Select most appropriate answer.

a) 0

b) false

c) -6

d) Compilation Error.Incompatible type for ~. Can't convert int to boolean.

e) ~6

f) -7

I would like to know why the answer is (f) and how do you approach this problem

public class Base{

private void test() {

System.out.println(~6);

}

static public void main(String[] a) {

new Base().test();

}

}

Select most appropriate answer.

a) 0

b) false

c) -6

d) Compilation Error.Incompatible type for ~. Can't convert int to boolean.

e) ~6

f) -7

I would like to know why the answer is (f) and how do you approach this problem

Thandapani Saravanan

Ranch Hand

Posts: 117

Christian Strohmaier

Greenhorn

Posts: 8

posted 15 years ago

- 0

A small rule of thumb for getting the bit representation of negative numbers (useful for >> >>> << questions):

1. consider the absolute value of the number

2. decrement this number

3. get the bit representation

4. invert every bit

Example:

bit representation of byte -14?

1. 14

2. 13

3. 00001101

4. 11110010

1. consider the absolute value of the number

2. decrement this number

3. get the bit representation

4. invert every bit

Example:

bit representation of byte -14?

1. 14

2. 13

3. 00001101

4. 11110010

Tony Alicea

Desperado

Sheriff

Sheriff

Posts: 3226

5

posted 15 years ago

- 0

You may have to learn binary for this. For economy of display space, I'll use the integral <CODE>byte</CODE> which occupies, as you know, eight bits:

the number 6 is

00000110

in binary.

is

11111001

which is a negative number because the most significant bit is '1'.

That happens to be -7.

to find the negative of a positive number, flip all bits and add one:

7 = 00000111

flipping all bits gives

11111000

adding one gives

11111001

which is -7.

the number 6 is

00000110

in binary.

*"not" 6*(that is,**~6**)is

**all bits inverted**:11111001

which is a negative number because the most significant bit is '1'.

That happens to be -7.

to find the negative of a positive number, flip all bits and add one:

7 = 00000111

flipping all bits gives

11111000

adding one gives

11111001

which is -7.

*Tony Alicea*
*Senior Java Web Application Developer, SCPJ2, SCWCD*

Gilbert Baron

Greenhorn

Posts: 23

posted 15 years ago

- 0

I at first did it this way too but there is a much easier way. You are using 2s complement notation here. Simply invert the bits and ad one. It is easier and one less step. It works in both directions. That is you can construct the negative number starting with the positive or determine what the negative number is starting with the negative

11111001

invert

00000110 = 6

add 1

= 7.

In reverse to constuct the negative 7

00000111

invert

11111000

add 1

11111001

Of course for positive numbers there is no problem (and no adding either)

00000110 is 6, period.

11111001

invert

00000110 = 6

add 1

= 7.

In reverse to constuct the negative 7

00000111

invert

11111000

add 1

11111001

Of course for positive numbers there is no problem (and no adding either)

00000110 is 6, period.

Originally posted by Christian Strohmaier:

A small rule of thumb for getting the bit representation of negative numbers (useful for >> >>> << questions):

1. consider the absolute value of the number

2. decrement this number

3. get the bit representation

4. invert every bit

Example:

bit representation of byte -14?

1. 14

2. 13

3. 00001101

4. 11110010

"Hierro candente, batir de repente"

M Bala

Greenhorn

Posts: 10

Harwinder Bhatia

Ranch Hand

Posts: 150

Ana Abrantes

Ranch Hand

Posts: 43

posted 12 years ago

- 0

I learned that way and I think it's very easy:

~x = (x + 1) * (-1)

Add one then change the signal.

Examples:

~0 = (0 + 1) * (-1) = -1

~6 = (6 + 1) * (-1) = -7

~31 = (31 + 1) * (-1) = -32

~(-1) = (-1 + 1) * (-1) = 0

~(-2) = (-2 + 1) * (-1) = 1

~(-9) = (-9 + 1) * (-1) = 8

~(-32) = (-32 + 1) * (-1) = 31

(this is the same as Bala and Harwinder said...)

[ November 20, 2003: Message edited by: Ana Abrantes ]

~x = (x + 1) * (-1)

Add one then change the signal.

Examples:

~0 = (0 + 1) * (-1) = -1

~6 = (6 + 1) * (-1) = -7

~31 = (31 + 1) * (-1) = -32

~(-1) = (-1 + 1) * (-1) = 0

~(-2) = (-2 + 1) * (-1) = 1

~(-9) = (-9 + 1) * (-1) = 8

~(-32) = (-32 + 1) * (-1) = 31

(this is the same as Bala and Harwinder said...)

[ November 20, 2003: Message edited by: Ana Abrantes ]

Ana<p>SCJP 1.4

Harwinder Bhatia

Ranch Hand

Posts: 150

Ana Abrantes

Ranch Hand

Posts: 43

posted 12 years ago

I tried to explain mathematically and it turned really awful, I agree with you. :roll:

What I meant is exactly you're version but I haven't realized it until I posted mine.

Let me try to correct myself.

That's my rule to solve this problem:

"get the number, add one to it and then change the sign."

~x => -(x + 1)

- 0

Originally posted by Harwinder Bhatia:

It's the same as Bala mentioned above, just a bit more convoluted

I tried to explain mathematically and it turned really awful, I agree with you. :roll:

What I meant is exactly you're version but I haven't realized it until I posted mine.

Let me try to correct myself.

That's my rule to solve this problem:

"get the number, add one to it and then change the sign."

~x => -(x + 1)

Ana<p>SCJP 1.4

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |