Skip to content

Commit

Permalink
update chap02
Browse files Browse the repository at this point in the history
2.9 completed
  • Loading branch information
SarveshMD committed Jul 28, 2021
1 parent 6581e0b commit 3260499
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions book/chap02.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ The unary operator `~` yields the one's complement of an integer; that is,
it converts each 1-bit into a 0-bit and vice versa. This operator typically
finds use in expressions like

x & ~ 077
x & ~077

which masks the last six bits of `x` to zero. Note that `x & ~077` is
independent of word length, and is thus preferable to, for example,
Expand All @@ -655,25 +655,25 @@ portable form involves no extra cost, since `~077` is a constant
expression and thus evaluated at compile time.

To illustrate the use of some of the bit operators, consider the function
`getbits(x, p, n)` which returns (right adjusted) the `n`-bit field of `x`
`getbits(x, p, n)` which returns (right adjusted) the n-bit field of `x`
that begins at position `p`. We assume that bit position 0 is at the right end
and that `n` and `p` are sensible positive values. For example,
`getbits(x, 4, 3)` returns the three bits in bit positions 4, 3 and 2, right
adjusted.

getbits(x, p, n) /* get n bits from position p */
unsigned x, p, n;
{
return((x >> (p+1-n)) & ~ ( ~ 0 << n));
}
getbits(x, p, n) /* get n bits from position p */
unsigned x, p, n;
{
return((x >> (p+1-n)) & ~(~0 << n));
}

`x >> (p+1-n)` moves the desired field to the right end of the word.
Declaring the argument `x` to be `unsigned` ensures that when it is right-
shifted, vacated bits will be filled with zeros, not sign bits, regardless of the
machine the program is run on. ~0 is all 1-bits; shifting it left `n` bit
positions with ~0 << `n` creates a mask with zeros in the rightmost `n` bits and
ones everywhere else; complementing that with `~` makes a mask with ones
in the rightmost `n` bits.
Declaring the argument `x` to be `unsigned` ensures that when it is
right-shifted, vacated bits will be filled with zeros, not sign bits,
regardless of the machine the program is run on. `~0` is all 1-bits;
shifting it left `n` bit positions with `~0 << n` creates a mask with
zeros in the rightmost `n` bits and ones everywhere else; complementing
that with `~` makes a mask with ones in the rightmost `n` bits.

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

Expand Down

0 comments on commit 3260499

Please sign in to comment.