Edit: Solved
I'm using an external makefile - a modified version taken from the LUFA VirtualSerial demo.
It uses the compiler option: "CFLAGS += -funsigned-char" which tells the compiler use unsigned char by default.
------------------------------------
I'm using Studio5 version 5.1.208.
I thought that char was signed 8-bit type by default, but when using itoa() it seems to be unsigned unless explicitly declared as signed.
Here's the code:
void TestBits(void) { ShowBits(-128); ShowBits(-1); ShowBits(0); ShowBits(1); ShowBits(127); } void ShowBits(signed char Num) { char i; char Buffer[9] = " "; char NumStr[9] = " "; itoa((int)Num, NumStr, 10); for(i=0; i<8; i++) { if (Num & (1 << i)) Buffer[7-i] = '1'; else Buffer[7-i] = '0'; } fputs("\n\r", &USBSerialStream); fputs(NumStr, &USBSerialStream); fputs(" = ", &USBSerialStream); fputs(Buffer, &USBSerialStream); fputs("\n\r", &USBSerialStream); }
And here's the results:
Using: void ShowBits(char Num) 128 = 10000000 255 = 11111111 0 = 00000000 1 = 00000001 127 = 01111111 Using: void ShowBits(signed char Num) -128 = 10000000 -1 = 11111111 0 = 00000000 1 = 00000001 127 = 01111111
The only difference between the two runs was changing char to signed char.
Did I get it backwards all these years?