# Laboratory: C's Bitwise Operations

Summary: In this lab, you will explore C's bitwise operations and some applications of those operations.

Contents:

## Exercises

### Exercise 1: Setting Selected Bits

Kernighan and Ritchie claim that

x &= ~077;

sets the last six bits of x to zero.

a. Explain their claim.

b. Write a program that helps you verify that claim. Your program should probably try that expression with a variety of numbers (which you can hardcode or take from the command line).

c. Explain how to set the last bit of x to zero. You may want to verify your answer with a variant of your program from step b.

d. Explain how to set the last bit of x to one. You may want to verify your answer with a variant of your program from step b.

e. Explain how to set the next-to-last bit of x to zero. You may want to verify your answer with a variant of your program from step b.

f. Explain how to set the next-to-last bit of x to one. You may want to verify your answer with a variant of your program from step b.

g. Explain how to set the last six bits of x to one. You may want to verify your answer with a variant of your program from step b.

h. Explain how to set the last seven bits of x to zero. You may want to verify your answer with a variant of your program from step b.

i. Explain how to set the last seven bits of x to one. You may want to verify your answer with a variant of your program from step b.

j. Explain how to set the last five bits of x to zero. You may want to verify your answer with a variant of your program from step b.

k. Explain how to set the last five bits of x to one. You may want to verify your answer with a variant of your program from step b.

Read the manual page for the Unix/Linux open system call (section 2 of the manual). How or why would one use bitwise operations in calls to this procedure?

### Exercise 3: Printing Binary Representations

Write a procedure, void printBits (int i), that prints the bits in i.

### Exercise 4: Right Shift

Kernighan and Ritchie note that

Right shifting a signed quantity will fill with sign bits (arithmetic shift) on some machines and with 0-bits (logical shift) on others.

Determine what happens on our machines.

### Exercise 5: Rotating Binary Representations

a. Write a function, int rightrot(int x, int n), that returns the value of the integer x rotated to the right by n bit positions. (This exercise is essentially the same as Exercise 2-8 of Kernighan and Ritchie.)

b. How or why might you use rightrot?

>

## History

Tuesday, 11 February 2003 [Samuel A. Rebelsky]

• Created.

Sunday, 12 September 2010 [Samuel A. Rebelsky]

Tuesday, 15 February 2011 [Samuel A. Rebelsky]

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue Feb 15 08:02:22 2011.
The source to the document was last modified on Tue Feb 15 08:02:16 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/bits-lab.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu