0

Here is the shiftOut function code from wiring_shift.c

void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
{
     uint8_t i;

     for (i = 0; i < 8; i++)  {
           if (bitOrder == LSBFIRST)
                 digitalWrite(dataPin, !!(val & (1 << i)));
           else      
                 digitalWrite(dataPin, !!(val & (1 << (7 - i))));

           digitalWrite(clockPin, HIGH);
           digitalWrite(clockPin, LOW);            
     }
}

Does this mean that least significant bit is on the left and most significant bit is on the right? Or LSBFIRST means that will be the last outputted bit so it will be "first" in outputted sequence?

One more thing

here is a sample code:

shiftOut(dataPin, clockPin, LSBFIRST, B00101001);
shiftOut(dataPin, clockPin, LSBFIRST, B11010011);

Why does this

union 
{
     word uniform;
     byte separate[2];
} state;

state.uniform = 54057; // aka 11010011 00101001
shiftOut(dataPin, clockPin, LSBFIRST, state.separate[1]);
shiftOut(dataPin, clockPin, LSBFIRST, state.separate[0]);

have different output but if i swap last two lines it works correctly?

  • 1
    You should read something about endianness. It'd work as you expect with big endian processor, but the majority is using little endian for the internal number representation. – KIIV Jan 23 at 10:48
  • ok, thank you for answer – Kekers_Dev Jan 23 at 10:50
0

Integer values of the Arduino have the MSB left and the LSB right. The shiftOut() function let's you choose, how the order should be timewise. If you provide LSBFIRST, the function will start the transmit with the LSB (on the right) and then go left to the MSB. Otherwise it will start with the MSB and then go right to the LSB. So this effects only the time order of the bits, in that they get transmitted.

  • Thank you for answer but there is one more problem, I've appended my question – Kekers_Dev Jan 23 at 10:14
0

There is no "left" or "right" in a binary number, only in the human representation of that number.

It is traditional for us to place the most significant bit on the left, purely because that his how we represent other numbers - like 1234 (1 is the most significant digit, the thousands).

Given the pseudo-number 12345678 if you shift out LSBFIRST the 8 gets output first, then the 7, then the 6, etc. In MSBFIRST the 1 gets output first, then the 2, then the 3, etc.

  • I would say, there is "left" or "right" in a binary number. There are even operators for shifting left or right. The human representation comes with the connection of "left" or "right" with LSB and MSB, so that the binary number can be converted to other bases or even printed out (since choosing where LSB/MSB is changes the representation in a different base, but not the binary number itself). – chrisl Jan 23 at 10:00
  • @chrisl There is only a left and right because we, as humans, decide to represent them that way. The shift left and shift right are again just names we have assigned to the operators. They should be called "SMS" and "SLS" for Shift Most-Significant and Shift-Least-Significant, but we choose to give these things chirality. – Majenko Jan 23 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.