Programming Practice

  1. A parity bit is a bit added to a binary stream to check the number of 1-bits in the stream is even or odd. For the case of even parity, if the number of 1-bits in the binary stream is odd, then the parity bit must be 1; if the number of 1-bits is even, the parity bit must be 0. For the case of odd parity, the value of the parity bit is reversed. Usually, a parity bit is attached at the end of a binary stream for checking whether there is an error bit in the binary stream or not. Write a C program to input a pair of parity and length in a line, where parity is a bit with value 0 denoting even parity and 1 denoting odd parity; length is the number of bits in the binary stream including the parity bit; and the next line is stream which is a binary stream as a sequence of hexadecimal digits (0 to F) with two nibbles spearated by a space such that the number of nibble pairs is élength/8ù. A nibble is a four-bit data. The stream starts from the most significant bit of the first nibble pair and the extra bits at the end are padded by 0's. Assume the stream length is at most 2000 bits (including parity bit). Count the number of 1 bit in the input stream and determine whether the input stream passes parity check, or not.  Repeat the parity check until the first two input is 0 0. You may use parity_check.txt as the sample input and modify the file as you like. A sample output is shown as below:

  2. Write a C program to simulate the combinational logic design of a 32-bit binary adder. A 32-bit binary adder is a logic circuite of 32 one-bit full adders. When adding two 32-bit positive integers, S=X+Y, a full adder takes xi, yi, and cin as input and produces si and cout, where xi, yi, and si are the i-th bit of X, Y, and S, respectively, and cin and cout are the input and output carry bit, respectively. The logic formula of a full adder is defined:

    si = (xi Å yi) Å cin

    cout = (xi Ù yi) Ú (cin Ù (xi Å yi))

    Refer to Digital System Design Lecture 12, Combinational Logic Design Binary Adder-Subtractor for more details of the logic design of binary adder. The program will repeatedly input two 32-bit unsigned integers X and Y, and use a binary adder to compute S=X+Y, until both X and Y are 0's. Do not use addition operation in C programming language. The output will print X, Y, and S in both decimal and binary format. Also, print a message to confirm that the binary adder has the same result as the addition operation of C programming language. If the addition results in the overflow situation, print an overflow message. A sample output is shown as below: