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.

290 Chapter 12 Operations on Bits<br />

In the third call to shift,a shift count of zero is specified. In this case, the shift function<br />

performs a right shift of value by zero bits, which, as you can see from the program’s<br />

output, has no effect on the value.<br />

The f<strong>in</strong>al pr<strong>in</strong>tf call illustrates nested function calls to the shift function.The<br />

<strong>in</strong>nermost call to shift is executed first.This call specifies that w1 is to be shifted right<br />

three places.The result of this function call, which is 0017777, is then passed to the<br />

shift function to be shifted to the left three places. As you can see from the program’s<br />

output, this has the net effect of sett<strong>in</strong>g the low-order three bits of w1 to 0.(Of course,<br />

you know by now that this could also have been done by simply AND<strong>in</strong>g w1 with ~7.)<br />

Rotat<strong>in</strong>g Bits<br />

For the next program example, which ties together some of the bit operations presented<br />

<strong>in</strong> this chapter, you will develop a function to rotate a value to the left or right.The<br />

process of rotation is similar to shift<strong>in</strong>g, except that when a value is rotated to the left,<br />

the bits that are shifted out of the high-order bits are shifted back <strong>in</strong>to the low-order<br />

bits.When a value is rotated to the right, the bits that are shifted out of the low-order<br />

bits of the value are shifted back <strong>in</strong>to the high-order bits. So, if you are deal<strong>in</strong>g with 32-<br />

bit unsigned <strong>in</strong>tegers, the value hexadecimal 80000000 rotated to the left by one bit produces<br />

hexadecimal 00000001 because the 1 from the sign bit that is normally lost by a<br />

left shift of one bit is brought around and shifted back <strong>in</strong>to the low-order bit.<br />

Your function takes two arguments: the first, the value to be rotated, and the second,<br />

the number of bits by which the object is to be rotated. If this second argument is positive,<br />

you rotate the value to the left; otherwise, you rotate the value to the right.<br />

You can adopt a fairly straightforward approach to implement<strong>in</strong>g your rotate function.<br />

For example, to compute the result of rotat<strong>in</strong>g x to the left by n bits, where x is of<br />

type <strong>in</strong>t and n ranges from 0 to the number of bits <strong>in</strong> an <strong>in</strong>t m<strong>in</strong>us 1, you can extract<br />

the leftmost n bits of x, shift x to the left by n bits, and then put the extracted bits back<br />

<strong>in</strong>to x at the right. A similar algorithm also can be used to implement the right rotate<br />

function.<br />

Program 12.4 implements the rotate function us<strong>in</strong>g the algorithm described previously.This<br />

function makes the assumption that an <strong>in</strong>t uses 32 bits on the computer.<br />

Exercises at the end of the chapter show one way to write this function so that this<br />

assumption does not have to be made.<br />

Program 12.4 Implement<strong>in</strong>g a Rotate Function<br />

// Program to illustrate rotation of <strong>in</strong>tegers<br />

#<strong>in</strong>clude <br />

<strong>in</strong>t ma<strong>in</strong> (void)<br />

{<br />

unsigned <strong>in</strong>t w1 = 0xabcdef00u, w2 = 0xffff1122u;<br />

unsigned <strong>in</strong>t rotate (unsigned <strong>in</strong>t value, <strong>in</strong>t n);

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

Saved successfully!

Ooh no, something went wrong!