Back
Close

Rust for Python Developers - Unsigned, Signed Integers and Casting

Shin_O
1,282 views

This story was originally published on Medium

Follow me:

Signed, Ones' Complement and Two's Complement

In computing, signed number representations are required to encode negative numbers in binary number systems. Let’s examine sign-and-magnitude, ones’ complement, and two’s complement.

Sign-and-Magnitude

Sign-and-Magnitude is also called Signed Magnitude. The first bit (called the most significant bit or MSB) tells if it is positive by 0 or a negative by 1. The rest is called magnitude bits.

4-bit-word

As I mentioned it before that signed integer types have the min and the max from -(2ⁿ⁻¹) to 2ⁿ⁻¹-1 where n stands for the number of bits. Since we use the first bit for the positive and negative signs we have n-1 in the 2ⁿ⁻¹.

For 4-bit the min and max are from -(2³) to 2³–1, which is -8 to +7.

As you see in the diagram above, the positive and the negative have the same digits except for the sign bit.

The problem of the signed magnitude is that there are two zeros, 0000 and 1000.

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