How do I print one bit?

后端 未结 6 412
野性不改
野性不改 2021-01-18 04:30

Please tell me how do I print a bit, like printf(\"%d\",bit);.

6条回答
  •  爱一瞬间的悲伤
    2021-01-18 05:19

    If you need to generalize more than Herms, you could do this:

    #define IsBitSet(val, bit) ((val) & (1 << (bit)))
    
    /* ... your code ... */
    
    printf ("%c", IsBitSet(bit, 0) ? '1' : '0');
    

    The printf is equivalent to Herms answer as is.

    If you're talking about bitfield in C, you can do this:

    struct foo { int b:1; } myFoo;
    
    printf("%c", myFoo.b ? '1' : '0');
    

提交回复
热议问题