2
private void test2() {
    // This test takes two shorts and sticks them together in a
    // 4 bit 12 bit configuration within a short, it then breaks
    // them apart again to see if it worked!
    short s0 = 4095;
    short s1 = 13;

    short sh = (short)((s1 << 12) | s0);

    System.out.println(sh);

    short[] sa = new short[] {
        (short)(sh & 0xFFF),
        (short)((sh >>> 12) & 0xF)
    };

    System.out.println(sa[0]);
    System.out.println(sa[1]);

}

What I expect from this is this;

s0 in binary is b0000_1111_1111_1111

s1 in binary is b0000_0000_0000_1101

sh then becomes b1101_1111_1111_1111

The preceeding 1 is the sign and the remaining 15 bits gives the value so sh in decimal is -24575 but this is not what I get outputted to the console (which is -8193).

What am I getting wrong?

3 Answers 3

6

The result is actually correct. Binary numbers are represents in what is called the 2s-complement. So to compute the absolute value of a negative number, you do not just remove the sign bit and see what remains. Rather you do this: 1. Flip all bits, including the sign bit 2. Add 1

In your case that means you get

  1. 0010_0000_0000_0000
  2. 0010_0000_0000_0001

Which is 8193, which is exactly what is printed out.

Sign up to request clarification or add additional context in comments.

1 Comment

I see, thanks for the explanation, I'll do some reading on the matter =)
6

b1101_1111_1111_1111 is -8193, it is outputting the correct answer. Might want to brush up on your 2s complements.

http://en.wikipedia.org/wiki/Two%27s_complement

5 Comments

My bad, I'm a Physics grad not Computer Science, thanks for your response.
indeed! There must be a reason for using 2s complements but at first glance it seems like the wrong choice lol! Thanks again.
Well think about how a computer sees it. If you subtract 1 from 0, you expect to get -1, but it would actually end up being 1111111... which would be the max negative int how you displayed it, but it is negative 1 as 2s complement.
The reason for using 2s-complement is because of the hardware implementation - when you use them, the computer can perform substractions with the same hardware it performs additions and no special treatment is necessary.
I'm just reading that wiki page and it seems that there are lots of advantages over the way I thought it worked, which I think is 1s complement. It's just one of those non-intuitive things that we must come to accept I suppose! Quantum mechanics confused me in the beginning but now it is the only way I can see the world; curse you Newton I believed in you!
2

The representation used is not sign-modulus, but yet 2-complement. Therefore, in order to know which number is represented by a sequence of bits that starts with one, you must subtract 1 and then invert. In your case you will get 1101_1111_1111_1110 inverted which will give 0010_0000_0000_0001 which is exatcly 8193. Therefore, there is no problem whatsoever - you just confused the nternal representation mechanism.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.