Docsity

Bitwise Operations - Computer Architecture II - Notes | CS 232, Exams of Computer Architecture and Organization

Toggle sidebar
bg1
CS232 Spring 2008 MP1: Bitwise Operations Due: 8pm Jan 23
“There are 10 different kinds of people in the world: those who know binary and those who don’t.”
– Anonymous
Learning Objectives
1. Bit-wise operators
2. The idea of treating integers as both numbers and bit arrays
3. Familiarization with a high-level language (HLL) code example that we will later translate
into MIPS
This MP is intended to give you a chance to acquaint yourself (or reacquaint yourself) with
the bit-wise logical operators and iteration in a higher level language, before we introduce these
concepts in MIPS.
It will also give you a chance to familiarize yourself with a chunk of C code that we will be
implementing in MIPS in later MPs.
Guidelines
•You may work alone, or in groups of up to three.
•Since your code is graded by our program, it is essential that you follow all directions. All of
your code should be below the ‘line’ in sudoku.c. Everything above the line will be removed
before we test your code.
•We will be testing your code using the EWS Linux machines, so we will consider what runs
on those machines to be the final word on correctness. Be sure to test your code on those
machines.
C Primer
In this MP, you will be programming in C. If you are already familiar with C and bitwise operations
in C, feel free to skip this section.
Compilation
Use the following command to compile bits.c. It will produce an executable called ”bits”. The
compiler will output any warnings that occur during compilation. Although it is not necessary to
turn in a code that compiles with no warnings, you may wish to eliminate all of them, as they may
be indicative of bugs.
g++ -Wall -o sudoku sudoku.c
1
pf3
pf4

Partial preview of the text

Download Bitwise Operations - Computer Architecture II - Notes | CS 232 and more Exams Computer Architecture and Organization in PDF only on Docsity!

“There are 10 different kinds of people in the world: those who know binary and those who don’t.”

  • Anonymous

Learning Objectives

  1. Bit-wise operators
  2. The idea of treating integers as both numbers and bit arrays
  3. Familiarization with a high-level language (HLL) code example that we will later translate into MIPS

This MP is intended to give you a chance to acquaint yourself (or reacquaint yourself) with the bit-wise logical operators and iteration in a higher level language, before we introduce these concepts in MIPS. It will also give you a chance to familiarize yourself with a chunk of C code that we will be implementing in MIPS in later MPs.

Guidelines

  • You may work alone, or in groups of up to three.
  • Since your code is graded by our program, it is essential that you follow all directions. All of your code should be below the ‘line’ in sudoku.c. Everything above the line will be removed before we test your code.
  • We will be testing your code using the EWS Linux machines, so we will consider what runs on those machines to be the final word on correctness. Be sure to test your code on those machines.

C Primer

In this MP, you will be programming in C. If you are already familiar with C and bitwise operations in C, feel free to skip this section.

Compilation

Use the following command to compile bits.c. It will produce an executable called ”bits”. The compiler will output any warnings that occur during compilation. Although it is not necessary to turn in a code that compiles with no warnings, you may wish to eliminate all of them, as they may be indicative of bugs.

g++ -Wall -o sudoku sudoku.c

Bit Arrays

In this MP, you will be dealing with bit arrays. In Java, one way to implement a bit array would be to simply declare a boolean array. Since C has no built-in boolean type, this option is not available to us. It is possible to just use an integer to store a bit, and then use an array of integers. However, we are going to view an integer as being a length 32 bit array. So, the nth bit in the array will be the nth bit of the integer. If we need a bit array longer than 32 bits, we can declare an array of integers, and then the first integer stores bits 0 through 31, the second integer stores bits 32 through 63, and so on.

Useful Operators

Some useful operators:

  1. << - the left shift operator. This is identical to the Java version, and will shift an integer value left the appropriate number of bits.
    • the right shift operator. Unlike the Java version, the C >> operator will either do >> (signed right shift) or >>> (unsigned right shift). Which one it does is dependent on the architecture and datatype being right shifted. When right shifting a value with the highest bit set, it would be a good idea not to make any sort of assumptions about whether a 0 or 1 is used to fill in the high bits.
  2. | - the bit-wise logical OR operator. You can view this as a SET operator. All of the bits set in the second argument will be set in the first argument. For example, x = x | 0x01 will set the first bit in x.
  3. & - the bit-wise logical AND operator. You can view this as a FILTER operator. This will return only the bits of the first argument which are specified by the second argument. For example, x & 0x01 will return the first bit of x.
  4. ~ - the bit-wise logical NEGATE operator. This will invert all of the bits of a value. You can use this along with the & operator to CLEAR bits. For example, x = x & ~0x01 will clear the first bit in x. ~0x01 will return a bit array with every bit except for the first one set, and then the & operator will filter out the first bit.

Problem

In this MP, you will complete a program that solves Sudoku puzzles; if you do not know what Sudoku is, please Google it now and then continue reading this assignment. The program uses bit-masks to represent the possible states for each square of the Sudoku puzzle. As each square can be any one of 9 numbers (1-9), we will use 9 bits (bits 0-8, where bit 0 is the least significant bit (LSB)) to represent which of the numbers each square can be. For example, if the value stored for a square is 0x14f where bits 0, 1, 2, 3, 6, and 8 are set, then this square can be the values 1, 2, 3, 4, 7, and 9 and not the values 5, 6, and 8. The key to solving Sudoku puzzles is eliminating possibilities for an unknown square until there is only one remaining possibility which must be the answer. We use to rules to eliminate possibilities:

if isum is not all possibilities: set board[i][j] to possibility not in isum changed = true

jsum = UNION of board[i][k] for k in 1 to 9, where k != j if jsum is not all possibilities: set board[i][j] to possibility not in jsum changed = true

ksum = UNION of board[x][y] for each square in 3x3 square not including (i,j) if ksum is not all possibilities: set board[i][j] to possibility not in ksum changed = true endfor endfor

Note: This is a challenging problem.

Testing

We strongly encourage you to test your code. We’ve provided some simple test case; you are encouraged to develop your own as well.

Submission

Hand in your code by using the handin program on any EWS Linux machine. The command to submit MP1 is:

handin -s mp

You will need to submit a sudoku.c and a partners.txt containing the netids of everyone in your group, one per line. Make sure these files are in the directory you run handin from. You may work alone, or in a group of up to three people. Each group should submit the MP through a single account. You can check the status of your submission by typing:

handin -i mp