Convert bitstring to byte in java

14,725

Solution 1

Binary literal were introduced in Java7.

Use following for older version:

byte b = Byte.parseByte("00000111", 2);

Solution 2

As the error message says, the 0b... syntax did not exist yet in Java 5 (which is what you seem to be using); it was introduced with Java 7. If you are using Java 7, make sure your compiler settings (in your IDE or build file) are set so that it accepts Java 7 syntax.

Bits are normally counted from the right to the left, so if you say bit 7 is 1, bit 6 is 1, etc. then I would expect the binary number to be 11100000 instead of 00000111.

To write this in source code in a Java version older than Java 7, you could simply write it as a hexadecimal or decimal number:

// Hexadecimal
byte extra_dop = (byte)0xE0; // or did you mean 0x07?

// Decimal
byte extra_dop = (byte)224; // or did you mean 7?

You could also use Integer.parseInt() with radix 2:

byte extra_dop = (byte)Integer.parseInt("11100000", 2);

(Note, you could also use Byte.parseByte but it will not accept 11100000 since it exceeds the range of the signed byte type).

Share:
14,725

Related videos on Youtube

user2656572
Author by

user2656572

Updated on September 15, 2022

Comments

  • user2656572
    user2656572 over 1 year

    I need to set each bit in 1 byte in Java.

    bit7 -  1,
    bit6 -  1,
    bit5 - 1,
    bit4 -  0,
    bit3 – 0,
    bit2 – 0,
    bit1 – 0,
    bit0 – 0
    

    I've written:

    byte extra_dop = 0b00000111;
    

    but got the following error:

    binary literals are not supported in -source 1.5 (use -source 7 or higher to enable binary literals)

    • Jon Skeet
      Jon Skeet over 10 years
      Well what version of Java are you using, and is using Java 7 an option?
  • Jon Skeet
    Jon Skeet over 10 years
    "That's not the way we do things in Java." That's far too general a statement. While BitSet is an option, using a single byte value is a perfectly legitimate option too.
  • Sean Patrick Floyd
    Sean Patrick Floyd over 10 years
    I agree that it's technically valid, but "Effective Java Item 47: Know and use the libraries" suggests that I should use existing solutions instead of reinventing them.
  • Sean Patrick Floyd
    Sean Patrick Floyd over 10 years
    since he wants primitive bytes, not Byte Objects, he should use parseByte instead of valueOf.
  • Jon Skeet
    Jon Skeet over 10 years
    It depends on the context. Sometimes it's appropriate to use BitSet, other times it isn't. Knowing the libraries exist allows you to make that decision. Suppose we're storing a million entries like this - creating a million BitSet objects may be prohibitively expensive compared with just creating a million-byte array. The OP apparently only wants to store 8 flags, so the "it can support more flags" isn't a benefit in this case. I think it's reasonable to suggest looking into it - but I'd have done that as a comment rather than an answer.
  • Amit G
    Amit G over 10 years
    With autoboxing never noticed difference. Thanks for pointing, updated answer.
  • Robert
    Robert over 6 years
    Byte.parseByte("11111111", 2); -> java.lang.NumberFormatException: Value out of range