Why use hex?

前端 未结 12 2008
你的背包
你的背包 2020-11-28 03:45

Hey! I was looking at this code at http://www.gnu.org/software/m68hc11/examples/primes_8c-source.html

I noticed that in some situations they used hex numbers, like i

12条回答
  •  孤城傲影
    2020-11-28 04:00

    I find it maddening that the C family of languages have always supported octal and hex but not binary. I have long wished that they would add direct support for binary:

    int mask = 0b00001111;
    

    Many years/jobs ago, while working on a project that involved an enormous amount of bit-level math, I got fed up and generated a header file that contained defined constants for all possible binary values up to 8 bits:

    #define b0        (0x00)
    #define b1        (0x01)
    #define b00       (0x00)
    #define b01       (0x01)
    #define b10       (0x02)
    #define b11       (0x03)
    #define b000      (0x00)
    #define b001      (0x01)
    ...
    #define b11111110 (0xFE)
    #define b11111111 (0xFF)
    

    It has occasionally made certain bit-level code more readable.

提交回复
热议问题