Question

I'm not very good with bit operations so I would like to ask the following: I have 4 bytes(bits), which can be either 0 or 1

byte a1=0;
byte a2=1;
byte a3=1;
byte a4=0;

How to create byte b , that has the following bits a1a2a3a4 0000 ?

Thank you in advance

Was it helpful?

Solution

Try this

byte a1=0;
byte a2=1;
byte a3=1;
byte a4=0;

byte b = (byte) ((a1 << 7) | (a2 << 6) | (a3 << 5) | (a4 << 4));

And see this documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top