開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
- Lubuntu 15.10 + gcc 5.2.1
- Raspbian + gcc 4.9.2
問題(Question):
我正在測試用RF module接收訊號然後轉成我要的char variable
大概是這樣:
輸入值:unsigned char (前4bit跟後4bit各代表數值a, b)
輸出值:兩個signed char
餵入的資料(Input):
unsigned char varible (0-255)
預期的正確結果(Expected Output):
char varible (-128-127)
錯誤結果(Wrong Output):
我用筆電+gcc 5.2.1跑出來數值是正確的
0, 0, 0, -1, -1
0, 1, 16, -1, 0
0, 2, 32, -1, 1
0, 3, 48, -1, 2
0, 4, 64, -1, 3
1, 0, 1, 0, -1
1, 1, 17, 0, 0
1, 2, 33, 0, 1
1, 3, 49, 0, 2
1, 4, 65, 0, 3
2, 0, 2, 1, -1
2, 1, 18, 1, 0
2, 2, 34, 1, 1
2, 3, 50, 1, 2
2, 4, 66, 1, 3
3, 0, 3, 2, -1
3, 1, 19, 2, 0
3, 2, 35, 2, 1
3, 3, 51, 2, 2
3, 4, 67, 2, 3
4, 0, 4, 3, -1
4, 1, 20, 3, 0
4, 2, 36, 3, 1
4, 3, 52, 3, 2
4, 4, 68, 3, 3
可是用RPi + gcc 4.9跑出來變成unsigned char
0, 0, 0, 255, 255 -> 255 應該是unsigned char才有可能
0, 1, 16, 255, 0
0, 2, 32, 255, 1
0, 3, 48, 255, 2
0, 4, 64, 255, 3
1, 0, 1, 0, 255
1, 1, 17, 0, 0
1, 2, 33, 0, 1
1, 3, 49, 0, 2
1, 4, 65, 0, 3
2, 0, 2, 1, 255
2, 1, 18, 1, 0
2, 2, 34, 1, 1
2, 3, 50, 1, 2
2, 4, 66, 1, 3
3, 0, 3, 2, 255
3, 1, 19, 2, 0
3, 2, 35, 2, 1
3, 3, 51, 2, 2
3, 4, 67, 2, 3
4, 0, 4, 3, 255
4, 1, 20, 3, 0
4, 2, 36, 3, 1
4, 3, 52, 3, 2
4, 4, 68, 3, 3
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <stdio.h>
int main(void) {
unsigned char a, b, c;
char d,e;
for (a=0; a<5; ++a) {
for (b=0; b<5; ++b) {
c = (b<<4)+a;
d = (char) (c&0xF) -1 ;
e = (char) ((c>>4) & 0xF) -1;
printf("%d, %d, %d, %d, %d\n", a,b,c,d,e);
}
}
return 0;
}
補充說明(Supplement):
請問這是gcc 4.9的bug嗎?
我不知道是不是該換成gcc5...