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

I think this question has already been discussed. -7 is bitwise complement of 6. Note that the operator ~ is supposed to reverse the bits of the operand.

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

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. "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

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.

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

This is the simplest technique (in my opinion): -x = ~(x - 1) (This is equivalent to what Bala mentioned above) Examples: -1 = ~(1 - 1) = ~0 -2 = ~(2 - 1) = ~1 -3 = ~(3 - 1) = ~2 so on and so forth ... Cheers Harwinder

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 ]

Ana<p>SCJP 1.4

Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150

posted

0

Originally posted by Ana Abrantes: I learned that way and I think it's very easy: ~x = (x + 1) * (-1)

It's the same as Bala mentioned above, just a bit more convoluted If you expand RHS: ~x = (x * -1) + (1 * -1) => ~x = -x - 1 (Same as Bala mentioned above) Enjoy ! Harwinder

Ana Abrantes
Ranch Hand

Joined: Sep 04, 2003
Posts: 43

posted

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)