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

  • Home
  • SEARCH
  • 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 7805511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:15:09+00:00 2026-06-02T02:15:09+00:00

I just wanted to understand this bit of code. static int convert2binary(int Decimal) {

  • 0

I just wanted to understand this bit of code.

static int convert2binary(int Decimal)
{
    int remainder, final = 0;
    string result = "";
    bool NaN;

    while (Decimal > 0)
    {
        remainder = Decimal % 2;
        Decimal /= 2;
        result = remainder + result;
        NaN = int.TryParse(result, out final);
    }
    return final;
}

It’s a binary converter, how does it work exactly? I just don’t get the modulus, decimal /= 2 and then plus them both together, how does that give binary?

  • 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-06-02T02:15:11+00:00Added an answer on June 2, 2026 at 2:15 am

    Let’s just input some data, OK?

    convert2binary(10)
    -> remainder, final = 0
    -> result = ""
    -> NaN (= false)
    
    loop:
    Decimal > 0, so: remainder = Decimal % 2 (= 0) and Decimal /= 2 ( = 5)
    result = remainder + result = 0 + ""
    NaN = false
    repeat:
    Decimal > 0, so: remainder = Decimal % 2 (= 1) and Decimal /= 2 ( = 2)
    result = remainder + result = "10"
    NaN = false
    repeat:
    Decimal > 0, so: remainder = Decimal % 2 (= 0) and Decimal /= 2 ( = 1)
    result = remainder + result = "010"
    NaN = false
    repeat:
    Decimal > 0, so: remainder = Decimal % 2 (= 1) and Decimal /= 2 ( = 0)
    result = remainder + result = "1010"
    NaN = false
    repeat: WHOOPS: Decimal == 0, so we return the final (int representation) of result.
    

    Now, why does this work?

    Basically, on each iteration, you split off the last binary digit from the right of your number (this is the %2 bit). Since you then divide the rest by 2 (the /=2 bit), you can do this in a loop.

    Each iteration will give you a successive position in the numbers polynom:

    decimal(10) == 1 * 2^3 + 0 * 2^2 + 1 * 2^1 + 0 * 2^0 = binary(1010)
    

    You can go in the other direction too: If you wanted to write an int.ToString() method for printing out the decimal variant of the number, you would split off the last digit with % 10 (the remainder of dividing the number by ten) and that is the rightest-most digit to print. Divide the rest by 10 to so you can repeat for the tens position, the hundreds position etc…

    lets try this out!

    int number = 123;
    // this is equivalent to: (1 * 10^2) + (2 * 10^1) + (3 * 10^0)
    int remainder = number % 10; // remainder = 3
    number /= 10 // number = 12 (integer division!!)
    result = remainder + ""; // result = "3"
    // number is now: (1 * 10^1) + (2 * 10^0), because we divided by 10!
    remainder = number % 10; // remainder = 2
    number /= 10 // number = 1
    result = remainder + result; // result = "23"
    // number is now: (1 * 10^0)
    remainder = number % 10; // remainder = 1
    number /= 10 // number = 0 - we're going to STOP now!
    result = remainder + result; // result = "123"
    // yay! hurray!!
    

    So, you see, your number system (be it binary or octal or decimal or hexadecimal or whatever) is just shorthand for writing down a polynom of powers of your base. The right-most digit is always base^0, and the exponent increases by one for each digit you move left.

    Bonus points if you figure out what the decimal point does 😉

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

Sidebar

Related Questions

I just wanted to understand if my method has some code that shows an
Just wanted to check to make sure that I understand this. A synchronized method
So, I just wanted to understand why this was happening in my WPF app
I just wanted to confirm if this was true or not - I remember
am new to this java world.. i just wanted to know how to convert
Trying to understand this binding process of the WPF. See the code at the
i am trying to understand this google social graph api a bit and i
I've just been doing a bit of wondering and learning, and I wanted to
I'm trying to understand Code Contracts in a bit more detail. I've got the
So I just wanted to ask why this works : let internal X th

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.