c - Bit-field: Understanding how the following program works -


can you, please, explain me case:

    struct registru {     int bit3:4;  };   struct registru bit={13}; printf("\n%d", bit.bit3); 

why result -3 ?

we need careful while using bit-fields. declared variable int, in c default signed int.

if see binary value of 13, 1101. msb taken sign value getting -3. if want take value 13 use below code:

struct registru {    unsigned int bit3:4;  };  void main() {    struct registru bit={13};   printf("\n%d", bit.bit3); } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -