8 Bits to byte -> how?

Hi!

I do have 8 Boolean values which should be converted to 1 byte. how can I do that?

like 10111010 -> 186

Background, I have to send those 8 bytes via Serial to a relays Card

tsaG

Serial.write (0b10111010);
or
Serial.write (0xBA); // 1011 1010 = BA
or
Serial.write (186); // maybe Serial.write (186, DEC); ??

I can also do it with bits, I think thats easier.

I have those 8 values

int R1=1;
int R2=0;
int R3=1;
int R4=1;
int R5=1;
int R6=0;
int R7=1;
int R8=0;

How do I convert those values to one byte?

byte b = R1 << 7 + r2 << 6+ r3 << 5 + r4 << 4 + r5 << 3 + r6 << 2 + r7 << 1 + r 8;

robtillaart:
byte b = R1 << 7 + r2 << 6+ r3 << 5 + r4 << 4 + r5 << 3 + r6 << 2 + r7 << 1 + r 8;

This doesn't work for me, its always 0 :frowning:

tsaG:

robtillaart:
byte b = R1 << 7 + r2 << 6+ r3 << 5 + r4 << 4 + r5 << 3 + r6 << 2 + r7 << 1 + r 8;

This doesn't work for me, its always 0 :frowning:

If you know that r1..r7 are always 0 or 1 and never anything else, try:

byte b = (r1 << 7) + (r2 << 6) + (r3 << 5) + (r4 << 4) + (r5 << 3) + (r6 << 2) + (r7 << 1) + r 8;

If r1..r8 could contain other values, you would probably want:

byte b = ((r1 != 0) << 7) + ((r2 != 0) << 6) + ((r3 != 0) << 5) + ((r4 != 0) << 4)
                   + ((r5 != 0) << 3) + ((r6 != 0) << 2) + ((r7 != 0) << 1) + (r8 != 0);

Thank you

I tried and ended with this :

int array[]={1,0,1,1,0,1,0,1};

void setup() {
  // initialize serial:
  Serial.begin(9600);
  

}

void loop() {
  byte value = 0;
for (int i = 0; i < 8; i++) {
  // trigger the clock pin and wait.
  if (array[i])
    value |= (1 << i);
}
  Serial.println(value);
  delay(1000);
}

Which works for me. Thank you all!

for (int i = 0; i < 8; i++) {
    value |= (!!array [i]) << i;

Not sure what the comment about a clock was all about.

tsaG:

    value |= (1 << i);

The bitWrite() function can be used to produce the same effect but without you needing to deal with bitwise operators directly:

byte value = 0;
for (byte i = 0; i < 8; i++)
{
    bitWrite(value, i, array[i]);
}

Why are you using 8 ints that hold one bit? At least cut the waste in half by using a byte array.

robtillaart:
byte b = R1 << 7 + r2 << 6+ r3 << 5 + r4 << 4 + r5 << 3 + r6 << 2 + r7 << 1 + r 8;

byte b = R1 << 7 | r2 << 6| r3 << 5 | r4 << 4 | r5 << 3 | r6 << 2 | r7 << 1 | r 8;

a variation on MichaelMeissner's

byte b = ((!!r1) << 7) + ((!!r2) << 6) + ((!!r3) << 5) + ((!!r4) << 4) + ((!!r5) << 3) + ((!!r6) << 2) + ((!!r7) << 1) + (!!r8);

using an array gives shorter code; (not necessary faster)

byte bb[8] = { 0,1,1,0,1,1,0,0 };
for (byte i=0; i<8;i++) b = b+ (bb[i]<<i);

I do have 8 Boolean values which should be converted to 1 byte. how can I do that?

...

tsaG:
I can also do it with bits, I think thats easier.

I have those 8 values

int R1=1;

int R2=0;
int R3=1;
int R4=1;
int R5=1;
int R6=0;
int R7=1;
int R8=0;




How do I convert those values to one byte?

They aren't booleans.

Read this:

for (int i = 0; i < 8; i++) {

value |= (!!array [i]) << i;

This is not likely to produce very good code on an AVR, because the only way it can shift is one bit at a time.
How about:

value = 0;
for (int i = 0; i < 8; i++) {
    value >>= 1;
    if (array[i])
        value |= 0x80;
}