Bit level operations, get a bit from a short value
Asked Answered
G

4

5

I'm trying to get the following result 00000000000000101 from this input 5.

This my code and it doesn't make sense:

public static int get16Bits(int x)
    {
        System.out.println((x & 0xffff) - ((x & 0x8000) << 1));
        return (x & 0xffff) - ((x & 0x8000) << 1);

    }

How i can get the 16bits of integer value?

Granule answered 22/9, 2017 at 12:44 Comment(5)
Which 16bits do you mean? Your question doesn't seem to make much sense. 00000000000000101 is just the binary representation of 5 with some leading zeros. So for this example input your method should just return the input as far as I guess.Ploughboy
Try this: String.format("%016d", Integer.parseInt(Integer.toBinaryString(x)))Bomke
@procrastinator This is what he wants? Nice, if you guessed correctly you should post an answer, I will upvote.Chromatography
@Ploughboy yup, i need the 16 bits binary.Granule
@Chromatography that's what I understood from the problem. Ain't sure whether they meant the same or different.Bomke
C
1

We can try this code below, we will use for loop :

public void getBits(int number){

      String bits = "";
      for (int i = 0; i < 16; i++) {
          bits = (number & 1) + bits;
          number >>= 1;
      }
      System.out.println("The bits are " + bits);
  }
Callis answered 22/9, 2017 at 15:11 Comment(0)
B
8

Try changing your return statement a bit:

    public static String get16Bits(int x)
    {
        return (String.format("%016d", Integer.parseInt(Integer.toBinaryString(x))));

    }

This should help you.

Bomke answered 22/9, 2017 at 12:52 Comment(4)
yup it's working. but not perfect... what about negative numbers ? also i need to get result without methods.Granule
change your method signature or your return type, because it does not compile.Earthy
@android without using any of the methods, it'd be difficult.Bomke
@procrastinator it would be mode easier and much funny and more useful.Granule
T
6

The first part is self-explanatory: the upper 16 bits are removed with & 0xffff masking.

Subtracting (x & 0x8000) << 1 is a trick that keeps the proper sign of the 16-bit number in the 32-bit result: for example, 0xffff, a -1 in 16-bit representation, gets converted to 0xffffffff, which is also -1 in 32-bit representation. This is better than using a conditional, because it is branch-free.

Another trick that you could use is

(((x & 0xFFFF) << 16) >> 16)

This lets the sign bit of 16-bit number "touch" the sign bit of 32-bit number, letting the right shift to sign-extend the result.

Note: If you are looking for a binary String representation of the number, only x & 0xffff masking is necessary, because the upper 16 bits are dropped from the string result anyway. This Q&A explains how to obtain a binary representation of an integer with the appropriate number of leading zeros.

Tribune answered 22/9, 2017 at 12:54 Comment(4)
Well, thanks for help, but it's still not perfect solution...! negative numbers didn't converted.Granule
@android Could you provide an example of a negative number that failed to convert?Tribune
-5 would be 1111111111111011Granule
@android That's the proper two's complement representation of negative five, though: you invert 0000000000000101 which becomes 1111111111111010, and add 1 to the result, producing 1111111111111011Tribune
C
1

We can try this code below, we will use for loop :

public void getBits(int number){

      String bits = "";
      for (int i = 0; i < 16; i++) {
          bits = (number & 1) + bits;
          number >>= 1;
      }
      System.out.println("The bits are " + bits);
  }
Callis answered 22/9, 2017 at 15:11 Comment(0)
F
0

Try below :

String.format("%16s", Integer.toBinaryString(5)).replace(' ', '0')
Fregger answered 22/9, 2017 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.