Question

write a subroutine that will count the number of 1’s in binary byte

write a subroutine that will count the number of 1’s in binary byte

Homework Answers

Answer #1

We can use AND operation for this purpose, we can just AND the number with 1 and get the value and keep on adding with the count until the value n becomes 0.

int count(int n){
int count = 0;
while(n>0){
count += (n&1);
n = n >> 1;
}
}

Thus the above subroutine will simply increase the count and make the n to be shift right so as to remove last significant bit.

That was a nice question to answer
Friend, If you have any doubts in understanding do let me know in the comment section. I will be happy to help you further.
Thanks

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
/* * bitSum8bit - returns count of number of 1's in a byte * Examples: bitSum8bit(257)...
/* * bitSum8bit - returns count of number of 1's in a byte * Examples: bitSum8bit(257) = 2, bitSum8bit(7) = 3 * Legal ops: & ( ) >> + * Do not use a loop! */ int bitSum8bit(int input) {   // These are all lines of code that you need.   int mask = ;   int quarterSum = ;   int mask2 = ;   int halfSum = ;   int mask3 = ;   return ; }
Write a program to count number between 0 and 255 in binary using 8 LEDs connected...
Write a program to count number between 0 and 255 in binary using 8 LEDs connected to the Arduino. This code should start at 0 and loop back to 0 once it counts to 255. Delay 0.25s between counts.
Write an assembly program to count the binary ones in register R1.   
Write an assembly program to count the binary ones in register R1.   
Write a piece of code to find the number of zeros in each byte of an...
Write a piece of code to find the number of zeros in each byte of an array of size 1024 byte stored at addresses starting at 21000H. Store the result of each byte in another array starting at 56000H.
IN C /* * bitParity8bit - returns 1 if the number of 1's in a byte...
IN C /* * bitParity8bit - returns 1 if the number of 1's in a byte is odd, * else it returns 0   * Examples: bitParity8bit(257) = 0, bitParity8bit(7) = 1 * Legal ops: & ( ) >> + * Do not use a loop! */ int bitParity8bit(int input) {   // These are all lines of code that you need.   int mask = ;   int quarterParity = ;   int mask2 = ;   int halfParity = ;   int mask3 = ;...
Write a program that reads a binary string (string of 0’s and 1’s) converting the binary...
Write a program that reads a binary string (string of 0’s and 1’s) converting the binary value into decimal. Allow the user to type in as many numbers as they want (one at a time) and end the program when they type in “0”. Use Horner’s method (given in step 3, below) to convert from binary to decimal. Sample Run Binary to Decimal Conversion Enter a binary string: 1101 1101 = 13 decimal Enter a binary string: 10011001 10011001 =...
code that will take a binary file and searching for a byte pattern and then print...
code that will take a binary file and searching for a byte pattern and then print to a new text file. code written in python. printing the data from the binary to human legible text. prints data when pattern in found.
C++ code please: Write a program to count the number of positive and the number of...
C++ code please: Write a program to count the number of positive and the number of negative inputs values. The program will stop when a 0 is input. Ex: If the input is -5.5 568 2.332 0 the output is 2 positive number(s) and 1 negative number(s). Ex: If the input is 153.0 0.534 2.2 5.6 46.584 0.015 5 0 the output is 7 positive number(s) and 0 negative number(s).
An 8-bit byte with binary value 11001101 is to be encoded using an even-parity Hamming code....
An 8-bit byte with binary value 11001101 is to be encoded using an even-parity Hamming code. What is the binary value after encoding?
write a subroutine at the end of the following code that will add two eight- bit...
write a subroutine at the end of the following code that will add two eight- bit number at data location 0x100 and 0x0200 and store the result in memory location x0x300?
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT