Question: Using C, complete the implementation of the set_bit, flip_bits, clear_bit, and toggle_bit functions. To complete the program, follow the instructions detailed in the in-line comments
Using C, complete the implementation of the set_bit, flip_bits, clear_bit, and toggle_bit functions. To complete the program, follow the instructions detailed in the in-line comments in the skeleton code below.
SAMPLE RESULTS:
$ ./sample_test_driver set_bit(01100001, 7) = 11100001 flip_bits(01100001 ) = 10011110 clear_bit(01100001, 7) = 01100001 toggle_bit(01100001, 7) = 11100001
set_bit(00111101, 2) = 00111101 flip_bits(00111101 ) = 11000010 clear_bit(00111101, 2) = 00111001 toggle_bit(00111101, 2) = 00111001
set_bit(00100101, 3) = 00101101 flip_bits(00100101 ) = 11011010 clear_bit(00100101, 3) = 00100101 toggle_bit(00100101, 3) = 00101101
set_bit(10000101, 4) = 10010101 flip_bits(10000101 ) = 01111010 clear_bit(10000101, 4) = 10000101 toggle_bit(10000101, 4) = 10010101
set_bit(00010001, 1) = 00010011 flip_bits(00010001 ) = 11101110 clear_bit(00010001, 1) = 00010001 toggle_bit(00010001, 1) = 00010011
Code:
#include
// Copy macros.h into your work directory if you want to use these macros. #include "macros.h"
typedef unsigned char byte;
byte set_bit(byte register_value, int pin) { // (a) Modify the register value by setting the designated pin // to 1 without altering the other pins, and return the result. // Hint: left shift; binary OR. }
byte flip_bits(byte register_value) { // (b) Flip all bits in the register value so that each bit that // is originally 1 becomes 0, and each bit that is originally 0 // becomes 1. // Hint: binary NOT. }
byte clear_bit(byte register_value, int pin) { // (c) Modify the register value by clearing the designated pin // (that is, setting it to 0) without altering any other pins, and // return the result. // Hint: left shift; binary NOT; binary AND. }
byte toggle_bit(byte register_value, int pin) { // (d) Modify the supplied register value by flipping the designated // pin, leaving all other pins unchanged. // Hint: left shift; binary XOR. }
//----------------------------------------------------------- #define REPEATS (5) #define BYTE_COUNT (256) #define PIN_COUNT (8) #define SEED ((unsigned) 314159) //-----------------------------------------------------------
/* ** Convert a byte to an integer with digits that look like the binary ** representation of the byte. ** ** For example: ** binstr(0x0c) == 1100, and ** binstr(0xcc) == 11001100. */ int binstr(byte b) { int result = 0;
for ( int i = 7; i >= 0; i-- ) { result = result * 10 + ((b >> i) & 1); }
return result; } //----------------------------------------------------------- int main(void) { srand(SEED);
for ( int i = 0; i < REPEATS; i++ ) { byte register_value = rand() % BYTE_COUNT; int pin = rand() % PIN_COUNT;
if ( i > 0 ) printf(" ");
printf(" set_bit(%08d, %d) = %08d ", binstr(register_value), pin, binstr( set_bit(register_value, pin))); printf(" flip_bits(%08d ) = %08d " , binstr(register_value), binstr( flip_bits(register_value ))); printf(" clear_bit(%08d, %d) = %08d ", binstr(register_value), pin, binstr( clear_bit(register_value, pin))); printf("toggle_bit(%08d, %d) = %08d ", binstr(register_value), pin, binstr(toggle_bit(register_value, pin))); } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
