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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:37:49+00:00 2026-06-10T20:37:49+00:00

For the sake of simplicity, let’s just assume the integer I am passing to

  • 0

For the sake of simplicity, let’s just assume the integer I am passing to this function is 9 which is 1001 in binary.

It has been my goal for a while now to write my own integer to binary function in C. The way I used to figure out binary values for number in shorthand was as followed (using 9 as mentioned above):

9 / 2 = 4.5 (remainder)            = 1
4 / 2 = 2   (no remainder)         = 0
2 / 2 = 1   (no remainder)         = 0
1 / 1 = 1   (remainder)            = 1

So if you reverse the 1 0 0 1 we get you will have the binary value of 9 which is still 1 0 0 1.

But then after looking over this site I found that the binary value of an integer can be found with some “simple” bitwise arithmetic. I found a function on another post on this site and adapted it into a function of my own:

char *itob(int integer)
{
    char *bin = 0X00, *tmp;
    int bff = 0;
    while(integer)
    {
        if(!(tmp = realloc(bin, bff + 1)))
        {
            free(bin);
            printf("\nError! Memory allocation failed while building binary string.");
            return 0x00;
        }
        bin = tmp;
        if(integer & 1) bin[bff++] = '1';
        else bin[bff++] = '0';
        integer >>= 1; 
    }
        bin[bff+1] = 0x00; 
    return bin;
}

Here is how I understand what is going on as well as my questions (that appear as comments)

1001 & 1 = 1 so put a 1 into the buffer //what is & doing that makes it equate to 1? Is it because the first digit in that sequence is a 1?
shift the bits in 1001 to the right one time
0010 & 1 != 1 so move a 0 into the buffer //same question as before is & just looking at the 0 because it is the first digit in the sequence?
shift the bits in 0010 to the right one time
0100 & 1 != 1 so move a 0 into the buffer //same question as before
shift the bits in 0100 to the right one time
1000 & 1 = 1 so put a 1 into the buffer //same question as before (at this point I'm thinking my theory is correct but I'm still not entirely sure)
shift the bits in 1000 to the right one time
loop ends

So as mentioned in my comments this is what I believe is going on in my program but I’m not 100% sure. Also I’m not sure if this is the best way to be even converting decimal to binary. (I’m already aware that if integer were for whatever reason to be a 0 I would eventually be trying to dereference a NULL pointer when trying to free the memory allocated by itob() along with a few other hiccups) But besides the questions that I already asked earlier is there a better method or more appropriate way to do this conversion?

  • 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-10T20:37:50+00:00Added an answer on June 10, 2026 at 8:37 pm

    No, the sequence of tests and shifts is

    1001 & 1 => 1   then shift right
     100 & 1 => 0   "
      10 & 1 => 0   "
       1 & 1 => 1   "
    

    The resulting integer 0 makes the loop terminate. So what this does is test each bit starting with the least significant bit and append a 0 or 1 in the buffer. That I’d say is backwards because when printed as a string the bit sequence is reversed from the one used most often, where the least significant bit is the rightmost one.

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

Sidebar

Related Questions

I have a dilemma. Let's assume(for simplicity's sake) I have four tables, with different
For the sake of simplicity, let's say that we have input strings with this
For the sake of simplicity, let's say my database has two tables, videos and
For sake of simplicity, let's assume I want to write an extension method for
for the sake of simplicity let's assume that I'm making a simple Pong clone
For the sake of simplicity, let's assume the following tables exist: Table 1 -
I am creating a custom WPF control that let's say for simplicity sake has
For the sake of simplicity, let's assume I'm cloning twitter (I'm not). So every
Let's say I have a utility function that, for simplicity's sake (the real thing
I'm working with three tables, and for simplicity's sake let's call them table A,

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.