Skip to content

Commit 3260499

Browse files
committed
update chap02
2.9 completed
1 parent 6581e0b commit 3260499

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

book/chap02.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ The unary operator `~` yields the one's complement of an integer; that is,
646646
it converts each 1-bit into a 0-bit and vice versa. This operator typically
647647
finds use in expressions like
648648

649-
x & ~ 077
649+
x & ~077
650650

651651
which masks the last six bits of `x` to zero. Note that `x & ~077` is
652652
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
655655
expression and thus evaluated at compile time.
656656

657657
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`
659659
that begins at position `p`. We assume that bit position 0 is at the right end
660660
and that `n` and `p` are sensible positive values. For example,
661661
`getbits(x, 4, 3)` returns the three bits in bit positions 4, 3 and 2, right
662662
adjusted.
663663

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+
}
669669

670670
`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.
677677

678678
[comment]: <> (page 46 , 46 THE C PROGRAMMING LANGUAGE CHAPTER 2 )
679679

0 commit comments

Comments
 (0)