Rust for Python Developers - Unsigned, Signed Integers and Casting

Shin_O
33.7K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

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.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content