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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:45:43+00:00 2026-06-11T06:45:43+00:00

I am coding something to decompose a number in binary on a training site.

  • 0

I am coding something to decompose a number in binary on a training site. I have test it a hundred of times on my local compiler, it works just fine, but the training site tells me there are errors.

(My code is nor elegant nor efficient, especially the loop but I decomposed the code to understand where the error could be). Could anybody tell me if there is an error ?

#include <stdio.h>
#include <stdlib.h>


//function that displays the greatest power of 2 less than a fixed number N
int residu(int N)

{
    int i;
    int M=2;
    for(i=0;i<N;i++){
        if(M>N){break;}else{M=2*M;i++;}
    }
    return M/2;
}


int main()
{
    int i;

    //N is the input to decompose
    int N;
    scanf("%d",&N);
    //We will search for the greatest power of 2 less than a fixed number N, 
    //than repeating the some process with the residue of N with the greatest power of 2        //less than N, so we have to store the value of N for the loop (see below) we will use to work //correctly
    int M;
    M=N;
    //D displays the diffrence betwenn two successive powers of 2 that appears in the    //binary decomposition, (we will then print "O")
    int D;
    D=log(residu(N))/log(2);

        for(i=0;i<M;i++){
            //If N==residu(N), the decomposition is finished
            if(N==residu(N)){printf("1");int k;
                for(k=0;k<D;k++){printf("0");}break;}
            else{
             // N is a the residue of the former value of N and the greatest power of 2 //less than N
                N=N-residu(N);
                D=D-log(residu(N))/log(2);
                printf("1");
                int k;
                for(k=0;k<D-1;k++){printf("0");
                }
                D=log(residu(N))/log(2);
            }        
    }
}
  • 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-11T06:45:46+00:00Added an answer on June 11, 2026 at 6:45 am

    This is a typical problem of floating point calculations. The function log works with floats.

    log(8) / log(2) is being calculated as 2.999... which is then truncated to 2 when being converted to int.

    That’s why you are getting wrong results. And the exact behavior is compiler/machine dependent. For further reading see e.g. Goldberg.

    It is in general a bad idea to mix integer and floating point calculations that way. Your function residu should report back the exact binary logarithm. Or you implement a dedicated function for calculating the log in integer, something like

    unsigned binlog(unsigned n) {
        unsigned i = 0;
        while (n > 1) { n /= 2; ++i; }
        return i;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am coding something in JS and I have to test code - I
I have an entity which looks something like this: (I'm coding to the web
I am coding something like an online PHP editor. So, now I have this
i am coding something like omegle random chat i have a table called chat_users
OK so I'm coding something and i have a left frame, a right frame,
I'm coding some web-applications in MVC, and I have a problem with something that
I have done some basic Perl coding but never something in python. I would
I am just coding something and wanted to know how I can include a
I'm coding something in PHP, I'm just using Ajax for the styling. basically I
Was coding something in Python. Have a piece of code, wanted to know if

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.