How to calculate the maximum block length in C of a binary number
- by user1664272
I want to reiterate the fact that I am not asking for direct code to my problem rather than wanting information on how to reach that solution.
I asked a problem earlier about counting specific integers in binary code. Now I would like to ask how one comes about counting the maximum block length within binary code.
I honestly just want to know where to get started and what exactly the question means by writing code to figure out the "Maximum block length" of an inputted integers binary representation.
Ex: Input 456 Output: 111001000
Number of 1's: 4
Maximum Block Length: ?
Here is my code so far for reference if you need to see where I'm coming from.
#include <stdio.h>
int main(void)
{
int integer; // number to be entered by user
int i, b, n;
unsigned int ones;
printf("Please type in a decimal integer\n"); // prompt
fflush(stdout);
scanf("%d", &integer); // read an integer
if(integer < 0)
{
printf("Input value is negative!"); // if integer is less than
fflush(stdout);
return; // zero, print statement
}
else{
printf("Binary Representation:\n", integer);
fflush(stdout);} //if integer is greater than zero, print statement
for(i = 31; i >= 0; --i) //code to convert inputted integer to binary form
{
b = integer >> i;
if(b&1){
printf("1");
fflush(stdout);
}
else{
printf("0");
fflush(stdout);
}
}
printf("\n");
fflush(stdout);
ones = 0; //empty value to store how many 1's are in binary code
while(integer) //while loop to count number of 1's within binary code
{
++ones;
integer &= integer - 1;
}
printf("Number of 1's in Binary Representation: %d\n", ones); // prints number
fflush(stdout); //of ones in binary code
printf("Maximum Block Length: \n");
fflush(stdout);
printf("\n");
fflush(stdout);
return 0;
}//end function main