@@ -646,7 +646,7 @@ The unary operator `~` yields the one's complement of an integer; that is,
646
646
it converts each 1-bit into a 0-bit and vice versa. This operator typically
647
647
finds use in expressions like
648
648
649
- x & ~ 077
649
+ x & ~077
650
650
651
651
which masks the last six bits of ` x ` to zero. Note that ` x & ~077 ` is
652
652
independent of word length, and is thus preferable to, for example,
@@ -655,25 +655,25 @@ portable form involves no extra cost, since `~077` is a constant
655
655
expression and thus evaluated at compile time.
656
656
657
657
To illustrate the use of some of the bit operators, consider the function
658
- ` getbits(x, p, n) ` which returns (right adjusted) the ` n ` -bit field of ` x `
658
+ ` getbits(x, p, n) ` which returns (right adjusted) the n -bit field of ` x `
659
659
that begins at position ` p ` . We assume that bit position 0 is at the right end
660
660
and that ` n ` and ` p ` are sensible positive values. For example,
661
661
` getbits(x, 4, 3) ` returns the three bits in bit positions 4, 3 and 2, right
662
662
adjusted.
663
663
664
- getbits(x, p, n) /* get n bits from position p */
665
- unsigned x, p, n;
666
- {
667
- return((x >> (p+1-n)) & ~ ( ~ 0 << n));
668
- }
664
+ getbits(x, p, n) /* get n bits from position p */
665
+ unsigned x, p, n;
666
+ {
667
+ return((x >> (p+1-n)) & ~(~ 0 << n));
668
+ }
669
669
670
670
` x >> (p+1-n) ` moves the desired field to the right end of the word.
671
- Declaring the argument ` x ` to be ` unsigned ` ensures that when it is right-
672
- shifted, vacated bits will be filled with zeros, not sign bits, regardless of the
673
- machine the program is run on. ~ 0 is all 1-bits; shifting it left ` n ` bit
674
- positions with ~ 0 << ` n ` creates a mask with zeros in the rightmost ` n ` bits and
675
- ones everywhere else; complementing that with ` ~ ` makes a mask with ones
676
- in the rightmost ` n ` bits.
671
+ Declaring the argument ` x ` to be ` unsigned ` ensures that when it is
672
+ right- shifted, vacated bits will be filled with zeros, not sign bits,
673
+ regardless of the machine the program is run on. ` ~0 ` is all 1-bits;
674
+ shifting it left ` n ` bit positions with ` ~0 << n ` creates a mask with
675
+ zeros in the rightmost ` n ` bits and ones everywhere else; complementing
676
+ that with ` ~ ` makes a mask with ones in the rightmost ` n ` bits.
677
677
678
678
[ comment ] : < > ( page 46 , 46 THE C PROGRAMMING LANGUAGE CHAPTER 2 )
679
679
0 commit comments