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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:59:19+00:00 2026-06-13T21:59:19+00:00

I am currently converting decimal to binary, making sure it is 8 bits. All

  • 0

I am currently converting decimal to binary, making sure it is 8 bits. All bit operations work except the ~ (NOT) operations. They come out as a huge integer value. I am not sure why, since the other bit operations work. Here is my code: (The commented out lines are what is not working)

Edit: If I want to get 8 bit binary strings, what do I do? Use unsigned chars? If I change all unsigned ints to unsigned chars then my BinaryToDecimal function produces incorrect binary conversion.

#include <iostream>
#include <string>

using namespace std;

string BinaryToDecimal(unsigned int dec)
{
    string binary   = "";
    float remainder = 0.0f;

    while( dec != 0 )
    {
        remainder = dec % 2;
        dec      /= 2;

        if( remainder == 0 )
            binary.append("0");
        else
            binary.append("1");
    }


    // Reverse binary string
    string ret = string(binary.rbegin(), binary.rend());

    return ret;
}

int main()
{
    unsigned int a = 0;
    unsigned int b = 0;

    cout << "Enter a number to convert to binary: ";
    cin  >> a;
    cout << "Enter a number to convert to binary: ";
    cin  >> b;

    cout << "A = " << BinaryToDecimal(a) << endl;
    cout << "B = " << BinaryToDecimal(b) << endl;

    unsigned int c = a & b;
    unsigned int d = a | b;
    //unsigned int e = ~a;
    //unsigned int f = ~b;
    unsigned int g = a ^ b;
    unsigned int h = a << 2;
    unsigned int i = b >> 3;

    cout << "A & B  = " << BinaryToDecimal(c) << endl;
    cout << "A | B  = " << BinaryToDecimal(d) << endl;
    //cout << "~A     = " << BinaryToDecimal(e) << endl;
    //cout << "~B     = " << BinaryToDecimal(f) << endl;
    cout << "A ^ B  = " << BinaryToDecimal(g) << endl;
    cout << "A << 2 = " << BinaryToDecimal(h) << endl;
    cout << "B >> 3 = " << BinaryToDecimal(i) << endl;
}
  • 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-13T21:59:20+00:00Added an answer on June 13, 2026 at 9:59 pm

    You are using an unsigned int for the operations, such that the inversion of small number becomes a large number because of leading 1 starting from the MSB. If you only want the representation is 8 bit only, you should use unsigned char for its storage.
    But you cannot change a or b to unsigned char. Otherwise, cin >> a will put the number’s ASCII code to a, not a number. For example, your input is 5, it puts 0x35 (‘5’), not number 5.

    If you don’t want to change unsigned int of your code, you can do some minor enhancements

    string BinaryToDecimal(unsigned int dec)
    {
        string binary   = "";
        float remainder = 0.0f;
    
        dec &= 0xff;        // only 8 bits you care about
        while( dec != 0 )
        {
            ....
    

    But you are using while( dec !=0 ), which is buggy. If the result is already 0, then the function returns an empty string, not “0000”. Instead, you should use a counter to count only for 8 bit.

        for (int i = 0; i < 8; i++ ) {
            if ((dec & 1) != 0)
                binary.append("1");
            else
                binary.append("0");
            dec >>= 1;
        }
    

    Also, using bit wise AND to test the bit is 0 or 1, and shift operation, is better than / and % operators.

    Finally, for 8 bit 5 (0000_0101), its inversion is 250 (1111_1010), not 1010.

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

Sidebar

Related Questions

I'm currently building a converter in C#. My program is converting from/to Decimal, Binary
I'm currently converting my portfolio mock up into html/css and I've come across something
I have an iPhone application that I am currently converting to a universal binary
Formatting large amounts of money is not an issue (e.g. converting a decimal to
I'm sorry if this is confusing....So far I'm converting a decimal number into binary.
I'm currently converting a Delphi method used for random binary file access into Java.
I'm currently converting a test class which extended the Spring class AbstractTransactionalSpringContextTests to Spring
I'm currently converting some legacy code to create PDF files using iTextSharp. We're creating
Currently working with converting SQLException error messages into messages that are more useful for
I am currently working on converting a ColdFusion website, using Fusebox framework, to PHP.

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.