Back
Close

Rust for Python Developers - Unsigned, Signed Integers and Casting

Shin_O
2,867 views

This story was originally published on Medium

Follow me:

Two's Complement

In the binary numbering system, the radix (base) is two. That’s why the radix complement is called two’s complement and the diminished radix complement is called ones’ complement.

Two’s complement can avoid multiple representations of 0, and it avoids keeping track of carrying bits in case of overflow.

4-bit word-3

We again use the first bit (MSB) for the + and - signs. We take the complement of the number and we add 1 to get the opposite number. This works from positive to negative and vice versa.

neg to pos

For zero that is 0000 in the binary, the complement is 1111 and adding 1 results in 1 0000. The 1 is called an "overflow" bit.

Overflow occurs when the sum of the most significant (left-most) column produces a carry forward. This overflow or carry bit can be ignored.

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants