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 582367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:40:45+00:00 2026-05-13T14:40:45+00:00

I am trying to print out binary number in c however the dilemma i

  • 0

I am trying to print out binary number in c however the dilemma i have is that its printing out in the reverse order. I have defined a function to tell me how many bits there are, this way i can work from the last bit back

to get the nth bit i can use

(value >> totalNumberOfBits) & 1;

in a while loop i can run this until the totalNumberOfBits == 0;

as such

while(totalNumberOfBits!= 0){
     putchar(n >> totalNumberOfBits)&1;
     totalNumberOfBits--;
}

any pointers would be welcome – i think i may be massivley off, i have an approach that prints them number fine backwards but iam trying to find a way of avoiding this

thanks

  • 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-13T14:40:45+00:00Added an answer on May 13, 2026 at 2:40 pm
    while (totalNumberOfBits != 0) {
         putchar(n >> totalNumberOfBits) & 1;
         totalNumberOfBits--;
    }
    

    Okay, you’re code was pretty close (and was in fact already printing out the bits in the correct order), however there were 3 small errors. Firstly, when compiling Visual Studio gives me the following warning:

    warning C4552: '&' : operator has no effect; expected operator with side-effect
    

    It is complaining about the & 1 part of your code, which you seem to have accidentally placed outside the parentheses of your putchar function call.

    while (totalNumberOfBits != 0) {
         putchar((n >> totalNumberOfBits) & 1);
         totalNumberOfBits--;
    }
    

    The second error is that, while it is now correctly printing bits, you are printing \0 and \1 characters. \0 will not show in the console, and \1 will most likely look like a smiley, so let’s fix that as well.

    while (totalNumberOfBits != 0) {
         putchar(((n >> totalNumberOfBits) & 1) ? '1' : '0');
         totalNumberOfBits--;
    }
    

    This is very close now, there is just one small mistake remaining. Because of the check your while loop performs, and the place where you decrement totalNumberOfBits, you never check the bit for 2^0, while you do check 2^8 even if your n is only 8 bits (and thus out of range). So we move the decrement, and substitute the !=:

    while (--totalNumberOfBits >= 0) {
         putchar(((n >> totalNumberOfBits) & 1) ? '1' : '0');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a function to print a number in binary using
I'm trying to come up with an algorithm that will print out all possible
I'm trying to print out a list of products on a page. This is
I am trying to print out true if there is such letter/word and false
I am trying to print out documents in various different orders. Right now I
I am trying to print out the names of each person in the loop.
I am having trouble with my Objective-C code. I am trying to print out
I'm using Django templating with Google App Engine. I'm trying unsuccessfully to print out
I'm basically trying to iterate through a dict and print out the key /
I'm trying to write a very simple program, I want to print out the

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.