Search This Blog

Blog Archive

Saturday, March 2, 2013

handy summary of digital Number formats:

one of my fellow students posted (anon.) this handy summary of
digital Number formats:
-------------------------------------------------------------------
8-bit unsigned
Range : 0 to 255
Note : Always positive . Start from 00000000 (0) to 11111111 (255).

Example :
69 = 01000101

8-bit signed magnitude
Range : -127 to 127
Note : Two zeros, +0 (00000000) and -0 (-00000000).
MSB is only the positive or negative sign, with 0 as positive and 1 as negative, contain no value.
Start from 10000000 (-127) to 01111111 (127).

Example :
+69 = 01000101
-69 = 11000101

8-bit One's Complement
Range : -127 to 127
Note : Two zeros, +0 (00000000) and -0 (11111111).
MSB is always negative and has value of -127. Start from 10000000 (-127) to 01111111 (127).

Example :
+69 = 01000101
-69 = 10111010

How to get this from signed number (!!) :
If MSB of signed number is 0 (positive), same as signed number
If MSB of signed number is 1 (negative), keep MSB as 1, invert the rest of the bits.

NB. on my current practice exam questions there are a number of questions such as the following:

Which of the following statements about the 8 bit binary number 10001111 is true ?
a) the number is odd when considered to be in signed magnitude, 2's complement, 1's complement, or excess 128 bit formats. 

In general a binary number ending in a zero is usually even unless it is in 1's complement format and its negative, in which case ending in a zero will cause it to be negative and a bin number ending in 1 will be positive, but for all other cases , as per normal - a bin ending in 0 will be even and ending in a 1 will be negative.


8-bit Two's Complement
Range : -128 to 127
Note : -128 written as 100000000, only one zero; +0 (00000000).
MSB is always negative and has value of -128. Start from 10000000 (-128) to 01111111 (127).

Example :
+69 = 01000101
-69 = 10111011

How to get this from signed number (?) :
If MSB of signed number is 0 (positive), same as signed number
If MSB of signed number is 1 (negative), keep MSB as 1, invert the rest of the bits, add 1 to the LSB.

Addition in 2's compliment format 

when adding bit sequences (on paper) and you have a 'carry bit' on the left at the MS end 
(most significant end) you can simply discard this carry bit.

8-bit excess-128
Range : -128 to 127
Note : From decimal number, add 128 to the value and write the 8-bit unsigned.
Start from 00000000 (-128) to 11111111 (127).

Example :
+69 = 69 + 128 = 197 = 11000101
-69 = -69 + 128 = 59 = 00111011

Binary to Octal/Hex and vice versa
Start from the right hand side (or the least significant bit) and group the bits in groups of three for octal, and four for hexadecimal. Then treat each group as an individual binary string and the result is the

Octal/Hexadecimal representation. 
For Binary strings that don't divide evenly into groups of three or four, just add as many zeros as needed to the left side.

Example :
1000101 can become 001 | 000 | 101 for the purpose of conversion (note the
added zeros to the left side)

Hence 69 (or 1000101) is 105 in Octal.

To convert Octal or Hexadecimal to Binary, all you need to do is expand each character into the appropriate number of binary digits (three for octal or four for Hex)

Also, if you want to convert from Hex to Octal or vise-versa, the easiest
way to do it is use conversion to binary in between.

3 comments:

Unknown said...

I remember the pain when I first learnt this, I would convert a 8 bit int and get 32 bit garble back, Now I declare all 8 bit integers as Uint8 if I can or pump it with a uint8 value first to set it.

dan said...
This comment has been removed by the author.
dan said...
This comment has been removed by the author.