The question is as follows: Write a function named CountBitTransitions() that re
ID: 3624014 • Letter: T
Question
The question is as follows:Write a function named CountBitTransitions() that returns the number of 0-to-1 bit transitions in the binary representation of its unsigned integer argument. Add your function to the following program, and run the completed program. For example, if x = 60599(10) = 1110110010110111(2), the number of 0-1 transitions is 4. Assume 16 bit binary representation.
#include <stdio.h>
int CountBitTransitions(unsigned int n);
int main( ) {
printf("# 01 trans in binary rep of %u = %d, should be 0 ", 1, CountBitTransitions(1));
printf("# 01 trans in binary rep of %u = %d, should be 1 ", 3, CountBitTransitions(5));
printf("# 01 trans in binary rep of %u = %d, should be 1 ", 0xFFFFFFFE,CountBitTransitions (0xFFFFFFFE));
printf("# 01 trans in binary rep of %u = %d, should be 5 ", 11530u, CountBitTransitions(11530u));
printf("# 01 trans in binary rep of %u = %d, should be 3 ", 40912,CountBitTransitions(40912));
printf("# 01 trans in binary rep of %u = %d, should be 0 ", 65535u,CountBitTransitions(65535u));
return 0;
/* Your zero bit count function goes here. */
}
I have written the C program and all the outputs are correct except for the 3rd one and i don't know why!!This is my code:
#include <stdio.h>
int CountBitTransitions(unsigned int n);
int main( ) {
printf("# 01 trans in binary rep of %u = %d, should be 0 ", 1, CountBitTransitions(1));
printf("# 01 trans in binary rep of %u = %d, should be 1 ", 3, CountBitTransitions(5));
printf("# 01 trans in binary rep of %u = %d, should be 1 ", 0xFFFFFFFE,CountBitTransitions (0xFFFFFFFE));
printf("# 01 trans in binary rep of %u = %d, should be 5 ", 11530u, CountBitTransitions(11530u));
printf("# 01 trans in binary rep of %u = %d, should be 3 ", 40912,CountBitTransitions(40912));
printf("# 01 trans in binary rep of %u = %d, should be 0 ", 65535u,CountBitTransitions(65535u));
return 0;
}
/* Your zero bit count function goes here. */
int CountBitTransitions(unsigned int n)
{
//First change the number n into binary and store the result in a character array
char binary[16];
int count=0;
unsigned int numToChangetoBase2 = n;
unsigned int exp = 1;
int k;
for (k=0; k<15; k++) {
exp = exp * 2;
}
for (k=15; k>=0; k--) {
if (numToChangetoBase2 >= exp)
{
binary[k]='1';
numToChangetoBase2 = numToChangetoBase2 - exp;
}
else
{
binary[k]='0';
}
exp = exp / 2;
}
for(k=15;k>0;k--)
{
if(binary[k]=='1'&&binary[k-1]=='0')
{
count++;
}
}
return count;
}
Explanation / Answer
The problem is that 0xFFFFFFFE does not fit into 16 bits. So, we'll ignore the top bits.
Instead of
unsigned int numToChangetoBase2 = n;
have
unsigned int numToChangetoBase2 = n&0xFFFF;
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.