'Notation of bits in C
I'm reading "The C Programming Language" and in the section on bitwise operations they write things like:
x & ~077
What do the 7s in 077 stand for?
Solution 1:[1]
In C, any number literal beginning with "0" and followed by digits between 0 and 7 (inclusive) is a value in octal notation. That is, the numeral base if 8.
So the number "077" has the decimal value of:
- 0 * 8²
- + 7 * 8¹
- + 7 * 8?
- = 0 + 56 + 7
- = 63
Or, as binary value with leading zeroes: "000111111".
The octal system was in wider use in former CS times, before the hex(adecimal) system took over. Both of these numeral systems have the advantage that each digit represents a set of bits. An octal digit stands for 3 bits, and a hex digit stands for 4 bits. A value in decimal notation cannot be "translated" simply digit-by-digit.
This is not the place to teach you the basics of number systems, you might want to read the Wikipedia page on positional notation to start with learning.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | the busybee |
