Data

Negative Numbers

Sign and magnitude (binary representation of negative numbers)

This is where the MSB is used as a SIGN (not value like twos complement) to represent whether or not a number is negative,

if it’s 1: it’s negative, 0: positive

This method is kinda ass for arithmetic operations hence twos complement is used more

Additionally there are two ways of representing 0, a negative zero and a positive one

Along with that, when u add like -28 and +28 (represented by sign and magnitude, we get -56 when it should be 0)

And since the bit that represents 128 is in use, the largest representable 8 bit number is 127

Twos complement,

Steps:

  1. flip all bits
  2. Add 1

How this works is that the MSB is actually negative.

Hence when u add up the place values (with the MSB being counted as negative), it becomes negative number as shown in the example below.

image.png

Shifting (Multiplying & Dividing)

Logical shifts (concept)

Note: Logical Shifts are used for Unsinged integers represented in binary

image.png

Arithmetic Shifts

Note: These are for Negative numbers represented by Twos complement

The Left shifts is Multiplication by multiples of two The Right shifts is Divison by multiples of two