<< Chapter < Page Chapter >> Page >

Unsigned integers

Unsigned integers are represented by a fixed number of bits (typically 8, 16, 32, and/or 64)

  • With 8 bits, 0…255 (0016…FF16) can be represented;
  • With 16 bits, 0…65535 (000016…FFFF16) can be represented;
  • In general, an unsigned integer containing n bits can have a value between 0 and 2 n 1 MathType@MTEF@5@5@+=feaagaart1ev2aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVCI8FfYJH8YrFfeuY=Hhbbf9v8qqaqFr0xc9pk0xbba9q8WqFfeaY=biLkVcLq=JHqpepeea0=as0Fb9pgeaYRXxe9vr0=vr0=vqpWqaaeaabiGaciaacaqabeaadaqaaqaaaOqaaiaaikdadaahaaWcbeqaaiaad6gaaaGccqGHsislcaaIXaaaaa@3970@

If an operation on bytes has a result outside this range, it will cause an ‘overflow’

Signed integers

The binary representation discussed above is a standard code for storing unsigned integer numbers. However, most computer applications use signed integers as well; i.e. the integers that may be either positive or negative.

In binary we can use one bit within a representation (usually the most significant or leading bit) to indicate either positive (0) or negative (1), and store the unsigned binary representation of the magnitude in the remaining bits.

However, for reasons of ease of design of circuits to do arithmetic on signed binary numbers (e.g. addition and subtraction), a more common representation scheme is used called two's complement. In this scheme, positive numbers are represented in binary, the same as for unsigned numbers. On the other hand, a negative number is represented by taking the binary representation of the magnitude:

  • Complement the bits : Replace all the 1's with 0's, and all the 0's with 1's;
  • Add one to the complemented number.

Example

+4210 = 001010102 and so-4210 = 110101102
  • Binary number with leading 0 is positive
  • Binary number with leading 1 is negative

Example

Performing two's complement on the decimal 42 to get -42

Using a eight-bit representation

42= 00101010 Convert to binary 11010101 Complement the bits11010101 Add 1 to the complement + 00000001-------- 11010110 Result is -42 in two's complement

Arithmetic operations on integers

Addition and subtraction of integers

Addition and subtraction of unsigned binary numbers

Binary Addition is much like normal everyday (decimal) addition, except that it carries on a value 2 instead of value 10.

0 + 0 = 0

0 + 1 = 1

1 + 0 = 1

1 + 1 = 0, and carry 1 to the next more significant bit

Example

00011010 + 00001100 = 00100110 1 1 carries0 0 0 1 1 0 1 0 = 26(base 10) + 0 0 0 0 1 1 0 0 = 12(base 10)---------------- 0 0 1 0 0 1 1 0 = 38(base 10)11010001 + 00111110 = 100011010 1 1 1 carries1 1 0 1 0 0 0 1 = 208 (base 10) + 0 1 0 0 1 0 0 1 = 73 (base 10)---------------- 1 0 0 0 1 1 0 1 0 = 281 (base 10)

The result exceeds the magnitude which can be represented with 8 bits. This is an overflow .

Subtraction is executed by using two's complement

Addition and subtraction of signed binary numbers

Multiplication and division of integers

Binary Multiplication

Multiplication in the binary system works the same way as in the decimal system:

0 x 0 = 0

0 x 1 = 0

1 x 0 = 0

1 x 1 = 1, and no carry or borrow bits

Example

00101001 × 00000110 = 11110110 0 0 1 0 1 0 0 1 = 41(base 10)× 0 0 0 0 0 1 1 0 = 6(base 10) ----------------------0 0 0 0 0 0 0 0 1 0 1 0 0 10 1 0 1 0 0 1 ----------------------------0 0 1 1 1 1 0 1 1 0 = 246(base 10) 00010111 × 00000011 = 010001010 0 0 1 0 1 1 1 = 23(base 10)× 0 0 0 0 0 0 1 1 = 3(base 10) ----------------------1 1 1 1 1 carries 0 0 1 0 1 1 10 0 1 0 1 1 1 0 0 1 0 0 0 1 0 1 = 69(base 10)

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to computer science. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10776/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?

Ask