Bytes to Binary in C

后端 未结 6 729
情书的邮戳
情书的邮戳 2021-02-04 14:22

I\'m trying to simply convert a byte received from fget into binary.

I know the value of the first byte was 49 based on printing the value. I now need to convert this in

6条回答
  •  说谎
    说谎 (楼主)
    2021-02-04 15:04

    You may notice that your output has a couple 1's and 0's, but also powers of 2, such as 32. This is because after you isolate the bit you want using the mask, you still have to bit-shift it into the least-significant digit so that it shows up as a 1. Or you could use what other posts suggested, and instead of bit-shifting the result (something like 00001000 for example), you could simply use (result != 0) to fetch either a 1 or 0, since in C, false is 0, and comparisons such as != will return 1 as true (I think).

提交回复
热议问题