I was looking in to the function shiftOut (below) to try and understand how the arduino board communicate with the shift register and it got me thinking about how all these bits are perceived by the program.
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);
}
}
As far as I can understand so far, all data manipulation made by the Arduino treats the data in a bitwise maner. Assuming that i understood it right, an operation like !!(val &(1<<5)) where val == 1001 1101 would be done as:
!!(1001 1101 &(0000 0001<<5))
!!(1001 1101 &(0001 0000))
!!(0001 0000) == 0000 0001
At this point I don't know if I understood the double negation right. Because if it was made bitwise, it should return 0001 0000 (or am I wrong?). But, as this operation is used to assign the value of the dataPin with digitalWrite I assumed that the end value should be 1 or 0. Is that it?
Another thing that confuses me is that the digitalWrite function is expecting a value "HIGH" or "LOW". And loking through the forum i read people saying that it is not a good idea to feed digitalWrite with a boolean variable. But isn't it what this function does?
The last thing that confuses me is the bitOrder variable. It i defined as unit8_t and it is compared whit a string of char (LSBFIRST). How would this string be read as unit8_t? Would each char be perceived as a 8 bit vector?