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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:08:20+00:00 2026-05-31T12:08:20+00:00

I need some explanation about the following piece of code: It is used for

  • 0

I need some explanation about the following piece of code:

It is used for converting decimal numbers to binary numbers code:
It is from a tutorial, but it puzzles me.

void binary(int);

void main(void) {
int number;

cout << "Please enter a positive integer: ";
cin >> number;
if (number < 0) 
    cout << "That is not a positive integer.\n";
else {
    cout << number << " converted to binary is: ";
    binary(number);
    cout << endl;
    //cin.get();
}
}

void binary(int number) {
int remainder;

if(number <= 1) {
    cout << number;
    return;
}

remainder = number%2;
binary(number >> 1);    
cout << remainder;
//cin.get();
}

I used breakpoint to watch the data go through the program but at the end I can’t follow it.

What i see:

It takes a number and if the number <= to 1 it prints that number (0 or 1).

But before it does it first calculate the modulus of that number and put that in remainder.
Then it moves a bit to the right of number or does the same until number is smaller or equal to 1.

But then it keeps “cout remainder” for several times (depending how much 0/1 there are calculated)
But how is this possible ?
Is remainder a buffer? (i thought it keeps overwritten(because it is int), but it looks like there keeps being bits added and then printed several times)???

Can someone explain this slowly to me ?

  • 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-31T12:08:22+00:00Added an answer on May 31, 2026 at 12:08 pm

    The int is not overwritten because it [the int] is re-allocated as an automatic variable for each time you invoke the function binary() recursively, so if you invoke it n times recursively, you actually allocate n different ints for remainder.

    So, it is “remembered” because they are different local variables. The allocation is usually made on the calls stack

    How it works: let’s have a look at the stack of an example: binary(5):

    |number=5, remainder = 1|
    -------------------------
    

    now, you reinvoke binary() with number = 5/2=2

    |number=2, remainder = 0|
    |number=5, remainder = 1|
    -------------------------
    

    and again with number = 2/2 = 1
    now, you reinvoke binary() with number = 5/2=2, and get:

    |number=1, remainder = 1|
    |number=2, remainder = 0|
    |number=5, remainder = 1|
    -------------------------
    

    Now, the stop condition is true, because number <= 1 -, so you print number [which is 1] and pop the first element from the call stack:

    |number=2, remainder = 0|
    |number=5, remainder = 1|
    -------------------------
    

    and print 0, since it is the remainder at the top of the calls stack.

    and do the same for the next “element” in the call stack:

    |number=5, remainder = 1|
    -------------------------
    

    and print the last remainder, 1.

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

Sidebar

Related Questions

Some explanation: there are several machines(hosts) that need to be notified about any changes
Need some regular expressions help. So far I have my code working to allow
Need some quick advice I am trying to access a object array but I
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
This question might seem very specific but I am in need of some ideas
I have done some web based projects, but I don't think too much about
This may be a stupid question for some of you but I need to
I need some help with one of the questions from my homework, Ive been
I have a relatively basic question but more than anything just need some clarity
[Check My EDIT for better Explanation] I need some help with a very big

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.