20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Exercises<br />

297<br />

Exercises<br />

1. Type <strong>in</strong> and run the four programs presented <strong>in</strong> this chapter. Compare the output<br />

produced by each program with the output presented after each program <strong>in</strong> the<br />

text.<br />

2. Write a program that determ<strong>in</strong>es whether your particular computer performs an<br />

arithmetic or a logical right shift.<br />

3. Given that the expression ~0 produces an <strong>in</strong>teger that conta<strong>in</strong>s all 1s, write a function<br />

called <strong>in</strong>t_size that returns the number of bits conta<strong>in</strong>ed <strong>in</strong> an <strong>in</strong>t on your<br />

particular mach<strong>in</strong>e.<br />

4. Us<strong>in</strong>g the result obta<strong>in</strong>ed <strong>in</strong> exercise 3, modify the rotate function from Program<br />

12.4 so that it no longer makes any assumptions about the size of an <strong>in</strong>t.<br />

5. Write a function called bit_test that takes two arguments: an unsigned <strong>in</strong>t and<br />

a bit number n.Have the function return 1 bit number n if it is on <strong>in</strong>side the<br />

word,and 0 if it is off. Assume that bit number 0 references the leftmost bit <strong>in</strong>side<br />

the <strong>in</strong>teger. Also write a correspond<strong>in</strong>g function called bit_set that takes two<br />

arguments: an unsigned <strong>in</strong>t and a bit number n.Have the function return the<br />

result of turn<strong>in</strong>g bit n on <strong>in</strong>side the <strong>in</strong>teger.<br />

6. Write a function called bitpat_search that looks for the occurrence of a specified<br />

pattern of bits <strong>in</strong>side an unsigned <strong>in</strong>t.The function should take three arguments<br />

and should be called as shown:<br />

bitpat_search (source, pattern, n)<br />

The function searches the <strong>in</strong>teger source,start<strong>in</strong>g at the leftmost bit, to see if the<br />

rightmost n bits of pattern occur <strong>in</strong> source. If the pattern is found, have the<br />

function return the number of the bit at which the pattern beg<strong>in</strong>s, where the leftmost<br />

bit is bit number 0. If the pattern is not found, then have the function return<br />

–1.So, for example, the call<br />

<strong>in</strong>dex = bitpat_search (0xe1f4, 0x5, 3);<br />

causes the bitpat_search function to search the number 0xe1f4 ( = 1110 0001<br />

1111 0100 b<strong>in</strong>ary ) for the occurrence of the three-bit pattern 0x5 (= 101 b<strong>in</strong>ary).<br />

The function returns 11 to <strong>in</strong>dicate that the pattern was found <strong>in</strong> the source<br />

beg<strong>in</strong>n<strong>in</strong>g with bit number 11.<br />

Make certa<strong>in</strong> that the function makes no assumptions about the size of an <strong>in</strong>t (see<br />

exercise 3 <strong>in</strong> this chapter).<br />

7. Write a function called bitpat_get to extract a specified set of bits. Have it take<br />

three arguments: the first an unsigned <strong>in</strong>t, the second an <strong>in</strong>teger start<strong>in</strong>g bit<br />

number, and the third a bit count. Us<strong>in</strong>g the convention that bit number<strong>in</strong>g starts

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!