The name of your C program file must be called mask.c, files with any other name will not be marked.
Bit masking is a technique that is frequently used in network and systems programming. Suppose we have a 4-bit binary string (i.e. a string consisting only of 0s and 1s) such as "0110", we can apply a 4-bit binary mask such as "1100" to produce a binary string "0100".
A 4-bit mask can be seen as a set of four adjacent gates in which each gate either allows the corresponding bit of the 4-bit string to pass through (i.e. 1) or blocks it (i.e. 0). A detailed explanation is provided below.
4-bit binary input : 0110 4-bit binary mask : 1100 -------------------------- 4-bit binary output : 0100 ^^^^ |||| |||+--- mask bit of 0 blocks corresponding bit 0 of input, ||| thereby resulting in output bit 0 ||| ||+---- mask bit of 0 blocks corresponding bit 1 of input, || thereby resulting in output bit 0 || |+----- mask bit of 1 allows corresponding bit 1 of input, | thereby resulting in output bit 1 | +------ mask bit of 1 allows corresponding bit 0 of input, thereby resulting in output bit 0
Write a program mask.c that accepts a 4-bit binary input string followed by a 4-bit binary mask, and displays the computed 4-bit binary output string. The following mask.c program has been provided for you that reads in the two input. Your task is to simply extend the program to compute and print out the masked output.
/**********************************************************************/ /* Matric Number: U051234A and U059876B */ /* Userid: u0501234 and u0509876 */ /* (For those doing pair programming, only ONE of you should submit */ /* your program. The other student should NOT submit.) */ /* Lab: 1 */ /* Lab Group Number: 99 */ /* Lab TA's Name: Who needs a lab TA when you're Bill Gates? */ /* Lab Session Date: 13 September 2006 */ /* Lab Session Time: 0800 - 0945 */ /* Title: Bit Masking */ /* Purpose: Perform 4-bit masking. */ /**********************************************************************/ #include <stdio.h> int main() { /* Variable declaration. You can declare any other variables here. */ int input, mask; /* Get user input. */ printf("Enter 4-bit binary : "); scanf("%d", &input); printf("Enter 4-bit mask : "); scanf("%d", &mask); /* Compute and print result here. */ return 0; }
Assuming that the executable is mask, a sample run of the program is shown below. User input is denoted in bold.
$ gcc -Wall mask.c -o mask $ mask Enter 4-bit binary : 0110 Enter 4-bit mask : 1100 -------------------------- 4-bit binary output : 0100 $ mask Enter 4-bit binary : 1010 Enter 4-bit mask : 0000 -------------------------- 4-bit binary output : 0000 $ mask Enter 4-bit binary : 0101 Enter 4-bit mask : 1111 -------------------------- 4-bit binary output : 0101 $ mask Enter 4-bit binary : 0000 Enter 4-bit mask : 0000 -------------------------- 4-bit binary output : 0000 $ mask Enter 4-bit binary : 1111 Enter 4-bit mask : 1111 -------------------------- 4-bit binary output : 1111 $ mask Enter 4-bit binary : 0111 Enter 4-bit mask : 0001 -------------------------- 4-bit binary output : 0001 $
A total of 17 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 7 times.
If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.