Concept of bit field

前端 未结 4 1177
故里飘歌
故里飘歌 2021-01-03 07:50
struct A
{
 int a:2;
 int b:3;
 int c:3;
};

int main()
{
 struct A p = {2,6,1};
 printf(\"\\n%d\\n%d\\n%d\\n\",p.a,p.b,p.c);
 return 0;
}    

Outp

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-03 08:34

    Your system seems to using 2's complement. A 2-bit bit-field holding 2 would be 10 in binary which is -2 in 2's complement system. Likewise 110(6) is -2 for a 3-bit representation in 2's complement. And 1 is plain 1

    Also read about signed bit-fields here

提交回复
热议问题