0

I read that in java, negative integers are obtained by taking 2's complement of a positive integer. In short, it also means that the higher order bit is being set to 1 in order to convert a positive integer to negative integer. Out of curiosity I tried figuring out exactly which bit in an integer acts as the higher order bit in java. Now, the limit of integers in java is -(2^32) to ((2^32) - 1). So I decided if I keep on checking each of the 32 bit positions, I will come to know which one is the higher order bit.
Following is the code I used.

public class Main
{
   public static void main(String[] args) {
      int x = 5;
      for(int i = 0; i<32; i++) {
        if((x|(1<<i)) == -5) {
            System.out.println(i + "th bit is the higher order bit");
        }
      }
   }
 }

But none of the bits came out to be the higher order bit. Which bit is it?

Anubhav Pandey
  • 1,285
  • 1
  • 14
  • 18
  • 4
    If you think that the binary representations of 5 and -5 differ only by the sign bit, you are mistaken. – Eran Dec 08 '20 at 08:34
  • Two's Complement is more than just signed magnitude. – Persixty Dec 08 '20 at 08:36
  • The highest order bit of an int is the 31st bit, which is ordinarily left-most when printing. – NomadMaker Dec 08 '20 at 08:45
  • 1
    Closed as a dupe because the question seems to be predicated on a misunderstanding of two's complement. – Andy Turner Dec 08 '20 at 08:50
  • Personally I think all Java questions should be closed as duplicates to "What is the Java Language Specification", and be done with it. This is exactly why I rarely answer Java questions these days - it's a waste of time. – Bathsheba Dec 08 '20 at 08:54
  • 1
    @Bathsheba I don't see why you reserve such contempt for Java; why aren't all language questions answered by their respective specifications? The problem isn't the spec, it's the squishy pulp trying to interpret it. – Andy Turner Dec 08 '20 at 09:02
  • @AndyTurner: No I have an affection for Java as stated on my profile. I'm programming in Java right now. My contempt is reserved for the zealous use of the duplicate close feature on the Java tag in particular. See the closed questions on Eran's profile for evidence. I'm not expecting folk to agree with me - I see it as a shame that folk decide to remove useful information in some quixotic pursuit of there being only one answer per flavour of question. – Bathsheba Dec 08 '20 at 09:21

1 Answers1

6

If you'll print the binary representation of 5and -5:

System.out.println (Integer.toBinaryString (5));
System.out.println (Integer.toBinaryString (-5));

You'll get:

101
11111111111111111111111111111011

or, if we add the leading 0s:

00000000000000000000000000000101
11111111111111111111111111111011

As you can see, the 2 representations differ in more than just the sign bit (which is the left most bit). Therefore your code is incorrect.

Setting the sign bit of the binary representation of 5:

System.out.println (5|(1<<31));

doesn't result in -5, it results in:

-2147483643
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Looks like the odds are improving in my favour? – Bathsheba Dec 08 '20 at 08:52
  • 1
    @Lino Indeed it isn't. But take a look at Eran's profile and to all the erudite answers of his that have been for questions that have been closed as duplicates of others. The Java tag is the one I used to follow that suffers from this misunderstanding of "duplicate" the most - by a country mile. It's that which I allude to. – Bathsheba Dec 08 '20 at 09:16
  • 1
    @Bathsheba You just taught me a new word - erudite. Thanks for that :) – Eran Dec 08 '20 at 09:18