Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

bit finding: manipulate binary value with precision Requirements: omplete the im

ID: 3742096 • Letter: B

Question

bit finding: manipulate binary value with precision

Requirements: omplete the implementation of the set-bit. flip bits, clear bit and toggle bit functions. The hash-tags for this exercise are: #cab2e After completing this exercise you will be able to manipulate bits with precision. These operations are required when programming at the hardware I To complete the program, follow the instructions detailed in the in-line comments in the skeleton code beloss Notes 2. and # Use this test driver to implement and test your function prior to submssion #include #include

Explanation / Answer

byte set_bit(byte register_value, int pin){

return register_value|= 1 << pin;

}

byte flip_bits(byte register_value){

return ~register_value;

}

byte clear_bit(byte register_value, int pin){

return register_value &= ~(1<<pin);

}

byte toggle_bit(byte register_value, int pin){

return register_value^= 1 << pin;

}

Explanation:

set_bit- left shift 1 to correct pin position then do bitwise OR with the register value. This will set the particular bit denoted by pin.

flip_bits- This is simply a bitwise not operation.

clear_bit- Shift 1 to correct pin and then bitwise NOT. This will make the number as all 1 except 0 in the pin position. Then do bitwise AND with register value. This will only change the pin position of the register value to 0, will not change any other bits.

toggle_bit- Left shift 1 to correct pin position then do bitwise XOR with register value so in the pin position the previous value will toggle.