7

I'm studying (ANSI) C by The C Programming Language (2nd Edition).

This is a code snippet from 2.10 Assignment Operators and Expressions:

1  /* bitcount() counts the number of 1-bits in its integer argument */
2  int bitcount(unsigned x)
3  {
4      int b;
5      for (b = 0; x != 0; x >>= 1)
6          if (x & 01)
7              b++;
8      return b;
9  }

I am confused why x & 01 is written in line 6 rather than x & 1 or x & 0x1? Is 0 before 1 necessary?

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
Kevin Dong
  • 5,001
  • 9
  • 29
  • 62
  • 1
    The section of the book may say "assignment operators" in the title, but the question has nothing to do with assignment operators, does it? – David K May 19 '14 at 17:48
  • @David K Yes. Please help me change the title to fit the question, thanks. :-) – Kevin Dong May 19 '14 at 17:49
  • 1
    The new title looks good to me now! – David K May 19 '14 at 17:56
  • 1
    It's used just for fast run code! `x & 01` or `x & 0x1` are fast than `x & 1` in running time. We can use `x /=2` instead `x >>=1` but this is lower running time than `x >>=1`. Also `x >>=01` is faster than `x >>=1`. –  May 19 '14 at 17:58
  • @KasiyA Why? Any reasons? :-) – Kevin Dong May 19 '14 at 18:00
  • @Kevin Dong Nai Jia:Ok read this link http://stackoverflow.com/questions/1168451/is-shifting-bits-faster-than-multiplying-and-dividing-in-java-net –  May 19 '14 at 18:04
  • @KasiyA The link you gave doesn't seem to say anything about the bitwise & operator, or the use of literal values with a leading zero. –  May 19 '14 at 18:11
  • 3
    @KasiyA that's not true. shifting is faster than dividing but the way you express the literal at the language level has no impact on the bitwise operation done with the **exact same value** at run time – Ryan Haining May 19 '14 at 18:16
  • By preceding a constant with `0`, it indicates an octal constant (as others have mentioned). It can _also_ affects the _type_. Depending on value, a _decimal_ constant will be type `int`, `long` or `long long` and an _octal/hexadecimal_ constant will be `int`, `unsigned`, `long`, `unsigned long`, `long long` or `unsigned long long`. With a value of 1, it make no difference though. But had the value been `4294967295`, using `037777777777` would have likely been `unsigned long`, and not potentially `long long` with `4294967295`. – chux - Reinstate Monica May 20 '14 at 18:55

3 Answers3

12

The 01 could also be written 1, or 0x1.

01  = Octal Constant  Base 8 (1 Decimal)
1   = Decimal Constant. Base 10
0x1 = Hexadecimal Constant. Base 16 (1 Decimal).

When the book was written, Base 8 (octal) was pervasive in existing computer programming.

Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
8

A leading 0 makes a constant into an octal (base 8) value.

In this case it's no different from 1 or 0x1 because they will all have the same binary representation of 000...001. People often use hex constants to distinguish that a value is being used as a bitmask or other bitwise value today. In the past, octal constants were more often used for the same purpose.

0x1  = hex constant = 1
0X1  = hex constant = 1
01   = octal contant = 1
1    = decimal constant = 1
1U   = unsigned decimal constant = 1
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
2

Indeed "01" is distinct from 1 and 0x1 in principle. "01" is encoded in octal. Kernighan/Ritchie probably value the ease in which one can convert from octal to binary for this particular problem.

Brian Cain
  • 14,403
  • 3
  • 50
  • 88