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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:54:59+00:00 2026-06-10T23:54:59+00:00

I need this function for some personal work and although doing this in general

  • 0

I need this function for some personal work and although doing this in general is trivial, I haven’t really played around with bit-shifting before. I have the following code, which tries to convert an integer into a vector of unsigned integers, where each is 1 or 0 (representing the bit). I can assume that the integers are always greater than or equal to 0. Here is my code:

vector<unsigned int> toBinary(int x)
{
    stack<unsigned int> s;
    vector<unsigned int> ret_val;

    for (unsigned i = 0; i < sizeof(x); ++i)
    {
        int z = ((x >> i) & 1) ? 1 : 0;
        s.push(z);
    }

    unsigned num_bits = s.size();
    for (unsigned i = 0; i < num_bits; ++i)
    {
        ret_val.push_back(s.top());
        s.pop();
    }

    return ret_val;
}

This works okay for integers up to 16, then it fails. I use the stack to reverse the order of the bits so the most significant bit is on the left in the final returned value. Why is this broken, and how can I clean it up? Thank you

  • 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-10T23:55:01+00:00Added an answer on June 10, 2026 at 11:55 pm

    x is an int – presumably 32 bits, i.e. 4 bytes, in size. sizeof x = 4, 4 bits allow for numbers 0..15

    What you want is 8*sizeof x, because of the 8 bits in a byte – although there is a better way (see below).

    Cleaning up-wise:

    1. Avoid using namespace std, if you must use it then narrow it down to single use (e.g. using std::vector). This means decorating things with std::, but it’s worth it for readability.

    2. Use unsigned int rather than int, be explicit about what you’re using.

    3. Returning a vector of bool seems to make more sense.

    4. The number of bits in a unsigned int is given by std::numeric_limits<unsigned int>::digits (#include <limits>).

    5. You can avoid the use of a stack easily enough by using std::reverse on the vector (#include <algorithm>).

    Putting it all together, your function could look something like:

    std::vector<bool> toBinary(unsigned int x)
    {
        std::vector<bool> ret_val;
    
        for ( unsigned int z = x; z > 0; z /= 2 ) {
    
            bool r = ( ( z & 1 ) == 1 );
            ret_val.push_back(r);
        }
    
        int bits = std::numeric_limits<unsigned int>::digits;
        ret_val.resize(bits);
    
        std::reverse( ret_val.begin(), ret_val.end() );
    
        return ret_val;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help in getting this right, problem Write a function which takes
I need to call some jQuery .load() function from flash. i Use this: import
I need to make this function work everywhere, except IE6-7 $(function(){ window.scrollTo(0,300); }) Please
I need to write some function using ML, this function receives the list of
I am creating a class for an application backbone and I need this function
i'm building this little function, what i need is this query to print me
I need this for calling a C function from Java class (JNI) and I
I have something like this: function showFunction () { // need position and place
I need to export my nodes like this: function recursive_simplify(node){ if(node.children){ for(var i =0;i<node.children.length;i++){
I need to add authentication to this function: function multiRequest($data, $options = array()) {

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.