CSC161 2011S Imperative Problem Solving
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Summary: In this lab, you will explore C's bitwise operations and some applications of those operations.
Contents:
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?
Write a procedure, void printBits (int i), that prints the
bits in i.
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.
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?
>
Tuesday, 11 February 2003 [Samuel A. Rebelsky]
Sunday, 12 September 2010 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/bits-lab.html.
Tuesday, 15 February 2011 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/bits-lab.html.
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
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.