Two Half Bytes to a Whole Byte

I'm shifting out 6 half bytes to three 595's. But it's just doing the first half byte then finishing the rest of the byte with 0. How do I stick together two half bytes to make a whole byte?

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
byte Dig[] = {
int i = 0;

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

void loop() {

if (i==10)i=0;
  digitalWrite(latchPin, LOW);

  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);

  digitalWrite(latchPin, HIGH);

Code, just in case its not clear what im trying to say. for example, with i = 4 i want to output "00110011,00110011,00110011" but instead im getting "00110000,00110000,00110000" then i would assume it does this twice since it has already gone through all three. But Im not 100% clear on how the 595 behaves...

anyway, yeah, just want to stick two half bytes together for output.

byte Digit = B00000000;

This works, obviously only for repeating each half byte twice, it would be slightly modified for actual use. Is there an easier way, or is this the way to get it done?

byte a, b, c;

c = (b << 4) | a;

This assumes bits 4 thru 7 of a are not set.

How it works:

If b = 00001111, b << 4 shifts those bits four bits to the left, so b = 11110000.
If a = 00001001 and you OR that with b, and place the result in c, then if a bit in a OR b is 1, the bit in c will be 1 as well, and c = 11111001.

I’m shifting out 6 half bytes to three 595’s.

  1. Each time you call shiftOut() It always shifts out eight bits.

  2. So: if you have three '595 devices connected in the usual manner, you need to call shiftOut three times.

  3. On Arduino, a variable whose data type is byte always has eight bits.

  4. So: if you want to shift the bits , say, 00110011, you can create a byte that has those bits and then call shiftOut. (See Footnote.)

  5. If you have a byte whose bits are 00000011 and you want to make upper 4 bits equal to lower four bits:

    byte x = B0011; // Same as if you had written byte x = B00000011;
    x = (x << 4) | x; // Assumes upper four bits are zero
   // If you don't know whether the upper four bits are zero and you want to make sure to throw them away
   x = (x << 4) | (x & B00001111);



Arrays always start with index equal to zero, so your array elements have the following values:

Dig[0] = B0000; // Same as B00000000 or, simply, 0
Dig[1] = B0001; // Same as B00000001 or, simply, 1
Dig[2] = B0010; // Same as B00000010 or, simply, 2
DIg[3] = B0011; // Same as B00000011 or, simply, 3
Dig[9] = B1001; // Same as B00001001 or, simply, 9

So you don’t need an array at all for these values, since the value of Dig[n] is, simply, n for n = 0, 1, …, 9


If you want to eliminate the necessity of having to use logical operations to duplicate the lower bits into the upper bits, you can define the elements of the array like this:

byte Dig[] = {

Then in the loop, shift Dig[ i ] three times to fill the three shift registers with that byte:

  digitalWrite(latchPin, LOW)
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  digitalWrite(latchPin, HIGH)

So, with i equal to 3, you will be sending B00110011 to each of the three shift registers.