C and numeric constants.

Recently, I came across a #define in my day job that was something like:

#define HEX_255 0xff

It made me think about all the various ways a number can be represented in C. Three of these are standard, but the binary representation is not (unless it’s been added to the standard since I last looked). I’ve found the binary version available in GCC and at least one embedded compiler I use.

// There are exactly the same.
unsigned int value1 = 255; // 255 in decimal
unsigned int value2 = 0xff; // 255 in hexadecimal
unsigned int value3 = 0377; // 255 in octal
unsigned int value4 = 0b11111111; // 255 in binary

printf ("%u %u %u %u\n", value1, value2, value3, value4);

It made me wonder … what does one use octal for? I remember being excited when I realized an old synthesizer I had was using octal for it’s sound selections (8 banks of 8 sounds). But beyond that, I don’t know if I’ve ever found octal being used anywhere. (I think this may have been on a Kawai K1/K4 synthesizer).

Anyone have any other examples?

2 thoughts on “C and numeric constants.

  1. David Brown

    Octal is used mostly when it makes sense to break a number into 3-bit packets, like, say, the permission bits used in a `chmod` command.

    There were also early machines (PDP-8, for example) that had 12 bit words, which can be nicely represented in 4 3-bit groups, so they used octal.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.