Programming in C

(Barry) #1

298 Chapter 12 Operations on Bits


at 0 with the leftmost bit, extract the specified number of bits from the first argu-
ment and return the result. So the call
bitpat_get (x, 0, 3)

extracts the three leftmost bits from x.The call
bitpat_get (x, 3, 5)

extracts five bits starting with the fourth bit in from the left.


  1. Write a function called bitpat_setto set a specified set of bits to a particular
    value.The function should take four arguments: a pointer to an unsigned intin
    which the specified bits are to be set; another unsigned intcontaining the value
    to which the specified bits are to be set, right adjusted in the unsigned int;a
    third intthat specifies the starting bit number (with the leftmost bit numbered 0 );
    and a fourth intspecifying the size of the field. So the call
    bitpat_set (&x, 0, 2, 5);


has the effect of setting the five bits contained in x, beginning with the third bit
from the left (bit number 2 ), to 0. Similarly, the call
bitpat_set (&x, 0x55u, 0, 8);

sets the eight leftmost bits of xto hexadecimal 55.
Make no assumptions about the particular size of an int(refer to exercise 3 in this
chapter).
Free download pdf