Convert bitstring to byte in java

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

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

I wrote:

byte extra_dop = 0b00000111;

but got the following error:

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

+4
source share
4 answers

The binary literal was introduced in Java7.

Use the following version for an older version:

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

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

, , , 7 1, 6 1 .., , 11100000 00000111.

Java Java 7, :

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

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

Integer.parseInt() radix 2:

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

( : Byte.parseByte, 11100000, byte).

+2

, Java. BitSet, .

Hmpfh. . Java - - , - , . BitSet, , , ?

0

, . , , , , , , , javac , java 7 ( ). , , , Netbeans Eclipse, ( , , t , IDE, , ).

However, if you want to edit the number later using bit operations, you will need to work like @Sean using BitSet (in fact, you can also use bit operations directly on numbers, as we do in C / C ++, it's simple not comfortable, but possible).

0
source

All Articles