105k views
5 votes
Please build your program in bit.c and part1.c, dont modify bit.h. Thanks

4. Your first task to to implement the function print_bits(). This function takes a single uint8_t value as a parameter and prints to stdout the value of the parameter in binary, starting with the most significant bit. It must always print out all 8 bits, so the value 110 should be displayed as 00000001 and the value 1610 should be displayed as 00010000. The file part1.c is the start of some testing for this function, you may wish to add more to this file if you wish.

bit.c

#include
#include
#include"bits.h"

void print_bits(uint8_t value)
{

}

uint8_t reverse_bits(uint8_t value)
{

}

uint8_t check_bit(uint8_t value, uint8_t bit)
{

}

uint8_t toggle_bit(uint8_t *value, uint8_t bit)
{

}

uint8_t get_subfield(uint8_t value, uint8_t start, uint8_t stop)
{

}

bit.h

#ifndef OSP_BITS_H
#define OSP_BITS_H
/* The above lines prevent multiple inclusion problems */

void print_bits(uint8_t value);

uint8_t reverse_bits(uint8_t value);

uint8_t check_bit(uint8_t value, uint8_t bit);

uint8_t toggle_bit(uint8_t *value, uint8_t bit);

uint8_t get_subfield(uint8_t value, uint8_t start, uint8_t stop);

#endif

Part1.c

#include
#include
#include"bits.h"


int main(int ac, char **av)
{
uint8_t val=0b10101010;

print_bits(val);

return 0;
}

User Amzad
by
7.9k points

1 Answer

1 vote

Final answer:

The print_bits() function in C can be implemented by iterating through each bit of a uint8_t number and printing it after bitwise manipulation to isolate and display each bit as '0' or '1'. Leading zeros are added for numbers with fewer than 8 bits.

Step-by-step explanation:

The task is to implement the function print_bits() in C programming language. This function will receive a single uint8_t value as a parameter and must print that value as an 8-bit binary string, with the most significant bit first. To ensure all 8 bits are always printed, leading zeros must be added for numbers that do not otherwise fill the full 8 bits.

Here's a possible implementation of the print_bits() function in the given bit.c file:

void print_bits(uint8_t value) {
for(int i = 7; i >= 0; i--) {
putchar('0' + ((value >> i) & 1)); }}
This code snippet uses a for loop to iterate through each bit of the input uint8_t number from the most significant to least significant bit. It shifts the value to the right 'i' times to bring the desired bit to the least significant position and then applies a bitwise AND operation with 1 to isolate it. The resulting bit is then printed as either '0' or '1'.
User Haron
by
8.3k points