Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7695743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:36:53+00:00 2026-05-31T21:36:53+00:00

This program is supposed to determine how many units are stored in the value

  • 0

This program is supposed to determine how many units are stored in the value of the variable c_val, if each unit is stored as a set bit.
My question is: why did the author write if (c % 2 == 1) count++; then shift c to the right with this statement c = c >> 1;?

#include <stdio.h>
#include <cstdlib>

int main(){
    unsigned char c_val;
    printf("char value = ");
    scanf("%c", &c_val);
    int count = 0;
    unsigned char c = c_val;
    while(c){
        if (c % 2 == 1) count++;
        c = c >> 1;
    }
    printf("%d bits are set", count);
    system("pause");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:36:54+00:00Added an answer on May 31, 2026 at 9:36 pm

    The data size of type char is always one byte – no exceptions. This code, however, calculates the popcount – that is, the number of 1 bits – in c_val.

    We can translate the relevant code from

    while (c) {
        if (c % 2 == 1) count++;
        c = c >> 1;
    }
    

    to

    while (c != 0) {
        if (c & 0x1 == 1) count++; /* if the rightmost bit of c is 1, then count++ */
        c = c / 2;
    }
    

    The last change I made works because right-shifting an unsigned integral data type (in this case, unsigned char) is equivalent to dividing by 2, with round-toward-zero semantics.

    We can think of c as a conveyor belt of bits – zero bits come in from the left, and one bit falls off the right on each loop iteration. If the rightmost bit is a 1, we increase the count by 1, and otherwise the count remains unchanged. So, once c is filled with zero bits, we know that we have counted all the one bits, and exactly the one bits, so count contains the number of one bits in c_val.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So my question is on this: This program is supposed to calculate the area
I have spent many hours working on this program that is supposed to play
Update this program is supposed to keep track of the number of times each
This program I use has it's own variables to set when you run it,
This program supposed to find command line arguments entered on Unix which ends with
This program is supposed to open a txt file, record the answers from two
So, this program is supposed to take stats from Auburn's football season and compute
This program is supposed to calculate the number of degrees below 60 on a
This program is supposed to run through a while loop twice. The first time
This program simply is supposed to eliminate duplicates from an array. However, the second

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.