Sign Up

Have an account? Sign In Now

Sign In

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 question.

Forgot Password?

Need An Account, Sign Up Here

You must login to add post.

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.

Passionable Logo Passionable Logo
Sign InSign Up

Passionable

Passionable Navigation

  • 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
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • New Questions
  • Trending Questions
  • Must read Questions
  • Hot Questions
Home/ Questions/Q 7609
In Process
Alek Richter
  • 0
Alek RichterEnlightened
Asked: December 27, 20212021-12-27T04:27:46+00:00 2021-12-27T04:27:46+00:00

What is “2’s Complement”?

  • 0

I’m in a computer systems course and have been struggling, in part, with Two’s Complement. I want to understand it but everything I’ve read hasn’t brought the picture together for me. I’ve read the wikipedia article and various other articles, including my text book.

Hence, I wanted to start this community wiki post to define what Two’s Complement is, how to use it and how it can affect numbers during operations like casts (from signed to unsigned and vice versa), bit-wise operations and bit-shift operations.

What I’m hoping for is a clear and concise definition that is easily understood by a programmer.

  • 1 1 Answer
  • 19 Views
  • 0 Followers
  • 0
    • Report
  • Share
    Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Alek Richter Enlightened
    2021-12-27T04:28:19+00:00Added an answer on December 27, 2021 at 4:28 am

    Two’s complement is a clever way of storing integers so that common math problems are very simple to implement.

    To understand, you have to think of the numbers in binary.

    It basically says,

    for zero, use all 0’s.
    for positive integers, start counting up, with a maximum of 2(number of bits – 1)-1.
    for negative integers, do exactly the same thing, but switch the role of 0’s and 1’s and count down (so instead of starting with 0000, start with 1111 – that’s the “complement” part).

    Let’s try it with a mini-byte of 4 bits (we’ll call it a nibble – 1/2 a byte).

    0000 – zero
    0001 – one
    0010 – two
    0011 – three
    0100 to 0111 – four to seven

    That’s as far as we can go in positives. 23-1 = 7.

    For negatives:

    1111 – negative one
    1110 – negative two
    1101 – negative three
    1100 to 1000 – negative four to negative eight

    Note that you get one extra value for negatives (1000 = -8) that you don’t for positives. This is because 0000 is used for zero. This can be considered as Number Line of computers.

    Distinguishing between positive and negative numbers

    Doing this, the first bit gets the role of the “sign” bit, as it can be used to distinguish between nonnegative and negative decimal values. If the most significant bit is 1, then the binary can be said to be negative, where as if the most significant bit (the leftmost) is 0, you can say the decimal value is nonnegative.

    “Sign-magnitude” negative numbers just have the sign bit flipped of their positive counterparts, but this approach has to deal with interpreting 1000 (one 1 followed by all 0s) as “negative zero” which is confusing.

    “Ones’ complement” negative numbers are just the bit-complement of their positive counterparts, which also leads to a confusing “negative zero” with 1111 (all ones).

    You will likely not have to deal with Ones’ Complement or Sign-Magnitude integer representations unless you are working very close to the hardware.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 4k
  • Best Answers 0
  • Users 2
  • Popular
  • Answers
  • Alek Richter

    Truth value of a Series is ambiguous. Use a.empty, a.bool(), ...

    • 2 Answers
  • Alek Richter

    What is a NullPointerException, and how do I fix it?

    • 2 Answers
  • Alek Richter

    Make MS Word document look like it has been typeset ...

    • 2 Answers
  • Alek Richter
    Alek Richter added an answer Pandas DataFrame columns are Pandas Series when you pull them… January 13, 2022 at 2:21 pm
  • Alek Richter
    Alek Richter added an answer The handshake failure could have occurred due to various reasons:… January 13, 2022 at 2:19 pm
  • Alek Richter
    Alek Richter added an answer Mac OS X doesn't have apt-get. There is a package… January 13, 2022 at 2:18 pm

Top Members

Alek Richter

Alek Richter

  • 4k Questions
  • 1k Points
Enlightened

Trending Tags

questin question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • New Questions
  • Trending Questions
  • Must read Questions
  • Hot Questions

© 2021 Passionable. All Rights Reserved

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.