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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:07:29+00:00 2026-06-02T15:07:29+00:00

I am trying to create a function to print a number in binary using

  • 0

I am trying to create a function to print a number in binary using bitwise and bit shifting but I am having trouble printing it correctly. The following is my code.

void PrintInBinary( unsigned int decNum )
{
    int i = 0;
    unsigned int highestOne = 1 << (sizeof(unsigned int)*8 - 1);

    for( i = 0; i < sizeof(int)*8; i++ ) {
         printf( "%u", decNum & (highestOne >> i) );
    }
    printf("\n");
}


int main()
{
    unsigned int a = 128;
    PrintInBinary( a );
    system("PAUSE");
    return 0;
}

The following is the output:

0000000000000000000000001280000000

Basically, its printing the 2^bit rather than just a 1 at each bit position (for example if I wanted to convert 7 to binary it would be 0000000…00421 instead of 0000000…00111). This is probably something trivial I’m missing, but any help guys? I’ve been at this for the last 20 min at can’t figure out something so simple.

  • 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-02T15:07:30+00:00Added an answer on June 2, 2026 at 3:07 pm

    Change decNum & (highestOne >> i) to (decNum & (highestOne >> i)) != 0.

    Many people also like to write !!(decNum & (highestOne >> i)). I concede it’s cute, but it’s less readable and I would suggest that you don’t use this.

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

Sidebar

Related Questions

I'm trying to create a function that searches up a WebControl's parent-child relationship (basically
I am trying to create a function which will give me alphabet position when
I'm trying to create a function to create a new basewith another struct as
I am trying to create a function that will block access to some of
I'm trying to create a function that would dynamically allocate an array, sets the
I am trying to create a function that given a divid, and a list
I'm trying to create a function that will simply allow me to pass an
I am trying to create a function which will take arguments arg1, arg2... then
I am trying to create a function that sends 100 call per second, and
I am trying to create a function to run when a select option is

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.