INTRODUCTION TO C PROGRAMMING CE00312-1 PRACTICAL No. 4 - Answers 1. Using the function displayBits shown below write a small program (task4a.c) to prompt the user to enter an unsigned integer from the keyboard and to display the number and its binary representation. Go on to form and display the one’s and two’s complements of the number. void displayBits(unsigned value) { unsigned c, displayMask = 1 << 31; printf("%7u = ", value); for (c=1; c<=32; c++) { putchar(value & displayMask ? '1' : '0'); value <<= 1; if (c % 8 == 0) { putchar(' '); } } putchar('\n'); } You are encouraged to keep the displayBits function in a separate header file since it could be accessed by the other tasks in the practical /* Task4a.c BITWISE DISPLAY AS BINARY */ /* using 32 bit integers */ #include <stdio.h> void displayBits(unsigned); int main(void) { unsigned x; x=255; while (x != 0 && x<4294967296) /* 2^32 */ { printf("Enter an unsigned integer: "); scanf("%u",&x); displayBits(x); } printf("\nDone!\n\n"); return 0; Page 1 } void displayBits(unsigned value) { unsigned c, displayMask = 1 << 31; printf("%7u = ", value); for (c=1; c<=32; c++) { putchar(value & displayMask ? '1' : '0'); value <<= 1; if (c % 8 == 0) { putchar(' '); } } putchar('\n'); } 2. Modify the code to create a new program (task4b.c) which allows for the entry of two unsigned integers. Carry out bitwise logical operations AND, OR and XOR on these numbers. Display and tabulate your results. /* Task4b.c BITWISE OPERATORS */ /* 32 bit integers */ #include <stdio.h> void displayBits(unsigned); */ function prototype */ int main(void) { unsigned number1,number2; printf("Number1: "); scanf("%u",&number1); printf("Number2: "); scanf("%u",&number2); printf("\n"); displayBits(number1); printf("\nAND\n"); displayBits(number2); printf("\n\n"); displayBits(number1 & number2); printf("\n\n\n"); displayBits(number1); printf("\nOR\n"); displayBits(number2); printf("\n\n"); displayBits(number1 | number2); printf("\n\n\n"); Page 2 displayBits(number1); printf("\nXOR\n"); displayBits(number2); printf("\n\n"); displayBits(number1 ^ number2); printf("\n\n\n"); printf("Done!\n\n"); return 0; } 3. Write a program bitshift.c that takes as input an unsigned integer and displays the number then the number shifted left and finally the number shifted right by a chosen number of bits. Your answer should print out the results in binary form although input can be in decimal form. /* BITWISE SHIFT OPERATORS */ /* using 32 bit integers */ #include <stdio.h> void displayBits(unsigned); int main(void) { unsigned number,bits; printf("Number: "); scanf("%u",&number); printf("How many bits to shift by? "); scanf("%u",&bits); printf("\n\nNumber is\n"); displayBits(number); printf("\nLeft Shift by "); printf("%u",bits); printf(" bits gives\n"); displayBits(number << bits); printf("\nRight Shift by "); printf("%u",bits); printf(" bits gives\n"); displayBits(number >> bits); printf("\nDone!\n\n"); return 0; } void displayBits(unsigned value) { unsigned c, displayMask = 1 << 31; printf("%7u = ", value); for (c=1; c<=32; c++) Page 3 { putchar(value & displayMask ? '1' : '0'); value <<= 1; if (c % 8 == 0) { putchar(' '); } } putchar('\n'); } 4. Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of an unsigned char variable y (leaving other bits unchanged). e.g. if x = 10101010 (170 decimal) and y = 10100111 (167 decimal) and n = 3 and p = 6 then you need to strip off 3 bits of y (111) and put them in x at position 10xxx010 to get answer 10111010. Your answer should print out the result in binary form although input can be in decimal form. Your output should be like this: x = 10101010 (binary) y = 10100111 (binary) setbits n = 3, p = 6 gives x = 10111010 (binary) /* FUNCTION SETBITS(x,p,y,n) */ /* using 32 bit unsigned integers */ #include <stdio.h> void displayBits(unsigned); int setBits(unsigned,unsigned,unsigned,unsigned); int main(void) { unsigned x,y,n,p; printf("\n\nNumber x: "); scanf("%u",&x); printf("Position p: "); scanf("%u",&p); printf("Number y: "); scanf("%u",&y); printf("Rightmost number of bits: "); scanf("%u",&n); printf("\n\nNumber x is\n"); displayBits(x); printf("\nNumber y is\n"); Page 4 displayBits(y); printf("\nSetting the rightmost %u bits in y to position %u in x gives:\n\n ", n,p); printf("Number x is now:\n"); x = setBits(x,p,y,n); printf("\n\nDone!\n\n"); return 0; } int setBits(unsigned x,unsigned p,unsigned y,unsigned n) { unsigned mask = y; mask = (mask << (32 - n)); mask = (mask >> (32 - p)); displayBits( x | mask ); return mask; } void displayBits(unsigned value) { unsigned c, displayMask = 1 << 31; printf("%7u = ", value); for (c=1; c<=32; c++) { putchar(value & displayMask ? '1' : '0'); value <<= 1; if (c % 8 == 0) { putchar(' '); } } putchar('\n'); } Page 5