Does anybody knows, Keil C compiler V7.xx supports bit fields with signed types or not? The code always generated same as for unsigned types. This is compiler bug or feature? My code compiled with Keil C fails on MCU, but works excellent on PC when compiled with gcc. Example: struct xxx { signed y: 7; }; struct xxx *s; volatile int i; i = s->y; /* This operator does not extends y to i with sign, instead all higher bits masked with zeros */ ; i = s->y; MOV DPTR,#s?25158 MOVX A,@DPTR MOV R3,A INC DPTR MOVX A,@DPTR MOV R2,A INC DPTR MOVX A,@DPTR MOV R1,A LCALL ?C?ILDPTR MOV R5,A MOV R4,B ANL A,#07FH MOV R7,A MOV DPTR,#i?25159 CLR A MOVX @DPTR,A INC DPTR MOV A,R7 MOVX @DPTR,A i=-2, s->y=i; /* This operator again only masking higher bits of i without carying of sign bit */ ; i=-2; MOV DPTR,#i?25159 MOV A,#0FFH MOVX @DPTR,A INC DPTR DEC A MOVX @DPTR,A ; s->y=i; MOV DPTR,#i?25159+01H MOVX A,@DPTR ANL A,#07FH MOV R7,A MOV A,R5 ANL A,#080H MOV R5,A MOV A,R4 MOV R6,A MOV A,R5 ORL A,R7 MOV R7,A MOV A,R6 MOV B,R7 LCALL ?C?ISTPTR
Bit fields in C51 (and many other C's for that matter) are an exercise in inefficiency. Avoid them like the plague.