Let's take your example above.
0b10011000 is binary notation (unsupported in C++, alas). In hex it would be 0x98, just as you wrote. Let's split it into four-bit nibbles for clarity.
When we shift the byte to the left (byte << 1), we literally do just that with the bit string.
The left shift in C fills the new bits at the right with 0 (which happens to match the shift_left call).
shift_left() had 1 for the 'bytes' parameter. Since we now have nine bits, we would have to drop the ninth one:
The dropped bit is "shifted out", and shift_left() returns its value.
Since we don't have shift_left(), we have to make an equivalent code. We know that the bit to be shifted out is the leftmost one (also called most significant bit), and we can test its value by ANDing our byte with 0x80 (0b10000000).
I hope this makes it somewhat clearer.
Hi All,
I have found a C code working with a PIC processor – 12C508A. I want that code to use with my mbed.
Since I am still learning mbed and C++, I need some help. if someone who familiar with that code syntax and convert it to the mbed syntax or point me a right direction how to make it by myself, I'll be really appreciated.
void sendbit(int thebit)
{
output_high(PICPIN); delay_us(400);
output_low(PICPIN); delay_us(700);
if (thebit == 0) { delay_us(1100); }
return;
}
void sendbyte(int thebyte)
{
int8 i;
for (i = 0; i < 8; i++)
{
sendbit(shift_left(&thebyte, 1, 0));
}
return;
}
void senddata()
{
int ufcbyte;
char hc;
hc=0b00000011;
hc=hc&0b10011000;
sendbyte(hc);
sendbyte(~hc);
ufcbyte=0b10000111;
ufcbyte=ufcbyte|0b01000000;
sendbyte(ufcbyte);
sendbyte(~ufcbyte);
sendbit(1);
return;
}
Sener