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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:33:47+00:00 2026-06-16T12:33:47+00:00

How can I tell if a binary number is negative? Currently I have the

  • 0

How can I tell if a binary number is negative?

Currently I have the code below. It works fine converting to Binary. When converting to decimal, I need to know if the left most bit is 1 to tell if it is negative or not but I cannot seem to figure out how to do that.

Also, instead of making my Bin2 function print 1’s an 0’s, how can I make it return an integer? I didn’t want to store it in a string and then convert to int.

EDIT: I’m using 8 bit numbers.

int Bin2(int value, int Padding = 8)
{
    for (int I = Padding; I > 0; --I)
    {
        if (value & (1 << (I - 1)))
            std::cout<< '1';
        else
            std::cout<<'0';
    }

    return 0;
}

int Dec2(int Value)
{
    //bool Negative = (Value & 10000000);

    int Dec = 0;
    for (int I = 0; Value > 0; ++I)
    {
        if(Value % 10 == 1)
        {
            Dec += (1 << I);
        }
        Value /= 10;
    }

    //if (Negative) (Dec -= (1 << 8));

    return Dec;
}

int main()
{
    Bin2(25);
    std::cout<<"\n\n";
    std::cout<<Dec2(11001);
}
  • 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-16T12:33:48+00:00Added an answer on June 16, 2026 at 12:33 pm

    You are checking for negative value incorrectly. Do the following instead:

    bool Negative = (value & 0x80000000); //It will work for 32-bit platforms only
    

    Or may be just compare it with 0.

    bool Negative = (value < 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know the binary representation of -127 is 10000001 (complement). Can any body tell
How can I tell if a file is binary (non-text) in Python? I am
Does Git store text compressed with Gzip as binary files or can it tell
so from what i can tell, you have to specify artifacts and working directory
I know that SortedDictionary is a binary search tree (and it can almost do
Possible Duplicate: Why can't decimal numbers be represented exactly in binary? I am trying
I have very little Perl experience. I need to read a binary image in
Can anyone please tell me how can I convert this float number: 12.25 to
Anyone can tell me which one is better (JAXB or Apache XMLBeans) taking in
Anyone can tell me what I'm doing wrong? I am creating a simple system

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.