Write the function printb() that takes a pointer to a 4 byte value and prints it
ID: 3871751 • Letter: W
Question
Write the function printb() that takes a pointer to a 4 byte value and prints its binary representation (preceded by 0b). Here's the Skeleton code in C:
#include <stdio.h> #include <stdlib.h> #include <string.h> // gcc e4.c -g -o e4 // $./e4 1 // $./e4 2 // $./e4 0.75 void printb(void *p); int main(int argc, char **argv) { int is_int = (strstr(argv[1], ".") == NULL); if (is_int) { int v = atoi(argv[1]); printb((void*)&v); } else { float v = atof(argv[1]); printb((void*)&v); } } void printb(void* p) { // FIXME: Complete this function so that it will print out the binary // representation (preceded by "0b") of the content point by ptr. // After you finish it, please test it. Some examples: // $./e4 1 // 0b00000000000000000000000000000010 or 0b10 // $./e4 2 // 0b00000000000000000000000000000001 or 0b1 // $./e4 0.75 // 0b00111111010000000000000000000000 or 0b111111010000000000000000000000 // Leading characters. printf("0b"); }Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// gcc e4.c -g -o e4
// $./e4 1
// $./e4 2
// $./e4 0.75
void printb(void *p);
int main(int argc, char **argv)
{
int is_int;
if (argc < 2)
{
printf("please enter command line argument ");
return -1;
}
is_int = (strstr(argv[1], ".") == NULL);
if (is_int) {
int v = atoi(argv[1]);
printb((void*)&v);
}
else {
float v = atof(argv[1]);
printb((void*)&v);
}
}
void printb(void* p)
{
// FIXME: Complete this function so that it will print out the binary
// representation (preceded by "0b") of the content point by ptr.
// After you finish it, please test it. Some examples:
// $./e4 1
// 0b00000000000000000000000000000010 or 0b10
// $./e4 2
// 0b00000000000000000000000000000001 or 0b1
// $./e4 0.75
// 0b00111111010000000000000000000000 or 0b111111010000000000000000000000
// Leading characters.
unsigned char c ;
int i,j;
printf("0b");
for (i = 3; i >=0; i--)
{
//get highest byte and dispaly its bits first till lower byte
c = ((char*)p)[i] ;
for (j = 0; j < 8; j++)
{
printf("%d", !!((c << j) & 0x80));
}
}
}
----------------------------------------------------------------------
//output
same as mentioned
./a.out 0.75
//output
0b00111111010000000000000000000000
./a.out 2
0b00000000000000000000000000000010
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.