0% found this document useful (0 votes)
104 views44 pages

Bits, Datatypes,& Operationg ch2

The document discusses data representation in computers. It explains that computers use binary digits (bits) to represent data, with each bit having a value of either 0 or 1. Different data types that can be represented include numbers, text, images, sound, logical values, and instructions. It also discusses how decimal and binary numbers are represented and converted between decimal and binary number systems. Finally, it covers different methods for representing negative numbers in binary, including signed magnitude, one's complement, and two's complement representations.

Uploaded by

logugl89
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views44 pages

Bits, Datatypes,& Operationg ch2

The document discusses data representation in computers. It explains that computers use binary digits (bits) to represent data, with each bit having a value of either 0 or 1. Different data types that can be represented include numbers, text, images, sound, logical values, and instructions. It also discusses how decimal and binary numbers are represented and converted between decimal and binary number systems. Finally, it covers different methods for representing negative numbers in binary, including signed magnitude, one's complement, and two's complement representations.

Uploaded by

logugl89
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 44

‫مبانی کامپیوتر و برنامه‬

‫سازی‬

Bits, Data types, and


Operations
Data Types

 Numbers
 Signed: -10, 5, 20, -123, …
 Unsigned: 10, 120, 2…
 Integers: 1, 2, 3, …
 Floating point: 2x10-12, 6.23x104 , …
 Complex: 2+j1 4.25+J6.23, …
 Rational: 2.3456, 10.239, …
 Text
 Characters: a, b, c, …
 Strings: “Tehran University”, “Bahman”, …
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 2
Data Types
 Images:
 Pixels:

 Shapes:
 Sound
 Logical
 True: “3>2”
 False: “6-3=5”
 Instructions
 Add two numbers, multiply 3 by 2, …
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 3
Decimal Numbers

 “decimal” means that we have ten digits to use in


our representation (the symbols 0 through 9)
 Decimal number 3,546?
 it is three thousands plus five hundreds plus four tens
plus six ones.
329
 Decimal number: 329:
102 101 100
3x100 + 2x10 + 9x1 = 329

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 4


Data representation in
computers
 At the lowest level, computers are
constructed by a bunch of simple switches.
 A computer is an electronic machine, and the
switches control the flow of electrons.

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 5


Data representation

 Easy to recognize two conditions:


1. Presence of a voltage – we’ll call this state “1”
2. Absence of a voltage – we’ll call this state “0”

 Could base state on value of voltage,


but control and detection circuits more complex.
 compare turning on a light switch to
measuring or regulating voltage

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 6


Digital data

 To make things even simpler we don’t count


on absolute presence or absence but the
closeness.
 Digital system:
 finite number of symbols

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 7


Symbols

 One bit has 2 possible states: 0, 1


 Two bits have 4 possible states:
B1 B0
0 0 0
1 0 1
2*2= 22 = 4
2 1 0
3 1 1

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 8


Symbols
 Three bits have 8 possible states:
B2 B1 B0
0 0 0 0
1 0 0 1
2 0 1 0
2*2*2= 23 = 8
3 0 1 1
4 1 0 0
5 1 0 1
6 1 1 0
7 1 1 1
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 9
Symbols

 N Bits have 2N states:


2 * …*2*2= 2N

 To represent K states we need:


K= 2N
N = log2 K

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 10


Binary Data

 Binary (base two) system:


 has two states: 0 and 1
 Basic unit of information is the binary digit,
or bit.
most least
 Binary number 101 significant 101 significant
22 21 20
1x4 + 0x2 + 1x1 = 5

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 11


Binary Conversion

 Binary to Decimal
Binary an….a2a1a0 = Decimal an*2n+ …. +a2*22 +a1*21+a0*20

Example:

101101B = 1*25+0*24 + 1*23 + 1*22 +0*21 +1*20


= 32+0+8+4+0+1
= 45 D
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 12
Binary Conversion
 Decimal to Binary
1. Divide by two – remainder is least significant bit.
2. Keep dividing by two until answer is zero,
writing remainders from right to left.
Example:
X = 104D
104/2 = 52 r: 0 bit 0
52/2 = 26 r: 0 bit 1
26/2 = 13 r: 0 bit 2
13/2 =6 r: 1 bit 3
6/2 =3 r: 0 bit 4
3/2 =1 r: 1 bit 5
1/2 =0 r: 1 bit 6
X = 1101000B
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 13
Unsigned Addition

 Just like Decimal numbers add from right to


left, propagating carry:carry
10010 10010 1111
+ 1001 + 1011 + 1
11011 11101 10000

10111
+ 111

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 14


Binary range

 With N bits we can represent numbers in the


range of:
0 ≤ i ≤ 2N - 1

 Problem: How to represent negative


numbers?

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 15


Signed Magnitude

 In decimal we use a ‘-’ in front of a number to


indicate negative.
 In binary we can use a leading bit to represent sign:
 0: Positive
 1: Negative
Example:
0 1101B = + (1*23 + 1*22 +0*21 +1*20) = 13D
1 1101B = - (1*23 + 1*22 +0*21 +1*20) = -13D
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 16
Signed Magnitude -4 10100
-3 10011
 Problems: -2 10010
• We have two numbers for zero (+/-)! -1 10001
• How do we do addition/subtraction?
-0 10000
+0 00000
+1 00001
+2 00010
+3 00011
+4 00100
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 17
One’s Complement

 The leading bit is used to represent sign:


 0: Positive
 1: Negative
 When negative invert all the bits
Example:
0 1101B = + (1*23 + 1*22 +0*21 +1*20) = 13D
1 1101B = - (0*23 + 0*22 +1*21 +0*20) = -2D

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 18


One’s Complement
-4 11011
-3 11100
 Problems: -2 11101
• We have two numbers for zero (+/-)! -1 11110
• How do we do addition/subtraction?
-0 11111
+0 00000
+1 00001
+2 00010
+3 00011
+4 00100
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 19
Two’s Complement

 The leading bit is used to represent sign:


 0: Positive
 1: Negative
 When negative invert all the bits and add 1 to the
result
Example:
0 1101B = + (1*23 + 1*22 +0*21 +1*20) = 13D
1 1101B = - (0*23 + 0*22 +1*21 +0*20 + 1) = -3D

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 20


-16 10000
Two’s Complement … …
-3 11101

Advantages: -2 11110
• Only one representation for zero -1 11111
• Operations need not check the sign 0 00000
• Efficient use of all the bits +1 00001
+2 00010
+3 00011
… …
+15 01111
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 21
‫‪Two’s complement addition‬‬

‫)‪00101 (5‬‬ ‫)‪01001 (9‬‬


‫)‪+ 11011 (-5‬‬ ‫)‪+10111 (-9‬‬
‫)‪00000 (0‬‬ ‫)‪00000 (0‬‬

‫مبانی کامپیوتر و برنامه سازی‬ ‫‪ ,‬پاييز ‪84‬‬ ‫‪22‬‬


Binary Conversion

 Binary to Decimal conversion of signed


numbers, for an….a2a1a0 :
 If an = 0, the magnitude of the number is:
an-1….a2a1a0 = an-1*2n-1+ …. +a2*22 +a1*21+a0*20
 if an = 1, obtain the 2’s complement of the number. This
will represent the magnitude.

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 23


Binary Conversion

 Decimal to Binary conversion of signed


numbers:
1. Divide by two – remainder is least significant bit.
2. Keep dividing by two until answer is zero,
writing remainders from right to left.
3. If the original number is positive append a zero
4. If the original number is negative obtain the 2’s
complement of the result and append it with a 1.

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 24


Arithmetic Operations

 Addition
 All numbers should have the same number of
bits
 Ignore carry out

01101000 (104) 11110110 (-10)


+ 00010000 ( 16) +11110111 (-9)
01111000 (120) 11101101(-19)

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 25


Arithmetic Operations
 Subtraction
 All numbers should have the same number of
bits
 Negate subtrahend (2nd no.) and add.
 ignore carry out
01101000 (104)
- 00010000 (16)
01101000 (104)
+ 11110000 (-16)
01011000 (88)
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 26
Sign extension
 To add two numbers, we must represent
them with the same number of bits.
 If we just pad with zeroes on the left:
4-bit 8-bit
0100 (4) 00000100 (still 4)
1100 (-4) 00001100 (12, not -4)
 Instead, replicate the MS bit -- the sign bit:
4-bit 8-bit
0100 (4) 00000100 (still 4)
1100 (-4) 11111100 (still -4)
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 27
Number Range

 Unsigned representation
0 ≤ i < 2N - 1
 Signed Magnitude
-2N-1 + 1 < i < 2N-1 - 1
 1’s Complement
-2N-1 + 1 < i < 2N-1 - 1
 2’s Complement
-2N-1 < i < 2N-1 - 1

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 28


Overflow
 If operands are too big, then sum cannot be
represented as an n-bit 2’s comp number.
01000 (8) 11000 (-8)
+ 01001 (9) + 10111 (-9)
10001 (-15) 01111 (+15)
 We have overflow if:
 signs of both operands are the same, and
 sign of sum is different.
 Another test -- easy for hardware:
 carry into MS bit does not equal carry out
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 29
Fractional Values

0.329
 In Decimal: 10-1 10-2 10-3
3x0.1 + 2x0.01 + 9x0.001 = 329

0.011
 In Binary 2-1 2-2 2-3
0x0.5 + 1x0.25 + 1x0.125 = 0.375

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 30


Fractional Operations

 2’s comp addition and subtraction still work


if binary points are aligned
2-1 = 0.5
2-2 = 0.25
2-3 = 0.125
00101000.101 (40.625)
+ 11111110.110 (-1.25)
00100111.011 (39.375)

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 31


Fractional Values

 Fixed point notation problem:


 Large values: 6.023 x 1023 -- requires 79 bits
 Small values: 6.626 x 10-34 -- requires >110 bits

 Use equivalent of “scientific notation”: F x 2E


 Need to represent F (fraction), E (exponent), and
sign.
 IEEE 754 Floating-Point Standard (32-bits)

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 32


Floating Point Representation
1 8 bits 23 bits
s exponent fraction

N = (-1)s x 1.fraction x 2(exponent – 127)


 Represent all exponent values as unsigned integers
 Example:

10111111010000000000000000000000

sign exponent fraction


 Sign is 1 – number is negative.
 Exponent field is 01111110 = 126 (decimal).
 Fraction is 0.100000000000… = 0.5 (decimal).
 Value = -1.5 x 2(126-127) = -1.5 x 2-1 = -0.75.
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 33
Floating Point

 Values represented by convention:


 Infinity (+ and -): exponent = 255 and fraction = 0
 NaN (not a number): exponent = 255 and fraction ≠ 0
 Zero (0): exponent = 0 and fraction = 0
 Exponent = 0 => fraction is de-normalized:

N = (-1)s x 0.fraction x 2(exponent – 127)

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 34


Double precision floating point

1 11 bits 52 bits
s exponent fraction

N = (-1)s x 1.fraction x 2(exponent – 1023)

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 35


Hexadecimal

 It is often convenient to write binary (base-2)


numbers as hexadecimal (base-16) numbers instead.
 fewer digits -- four bits per hex digit
 less error prone -- easy to corrupt long string of 1’s and
0’s
 0 to 9 represented as such
 10, 11, 12, 13, 14, 15 represented by A, B, C, D, E, F
 16 = 24: i.e. every hexadecimal digit can be represented
by a 4-bit binary (unsigned) and vice-versa.

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 36


Hexadecimal

 Every four bits is a hex digit.


– start grouping from right-hand side

011101010001111010011010111

3 A 8 F 4 D 7

This is not a new machine representation,


just a convenient way to write the number.

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 37


‫‪Hexadecimal‬‬

‫‪16AB16 = x16AB‬‬
‫‪= 1x163 + 6x16 2 + 10x16 + 11‬‬
‫‪= 580310 = #5803‬‬
‫‪= 0001 0110 1010 10112‬‬

‫مبانی کامپیوتر و برنامه سازی‬ ‫‪ ,‬پاييز ‪84‬‬ ‫‪38‬‬


Logical Operations

 0, 1 in a binary value can represent logical


TRUE = 1, or FALSE = 0.
 We can perform logical operations on binary
bits or a set of binary bits, also known as
Boolean algebra.
 The basic operations are AND, OR, NOT

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 39


Basic Logic Operations
Truth Tables of Basic Operations
AND OR
NOT A B A.B A B A+B
A A' 0 0 0 0 0 0
0 1 0 1 0 0 1 1
1 0 1 0 0 1 0 1
Equivalent Notations
1 1 1 1 1 1
 Not A = A’ = A
 A and B = A.B = A∧B = A intersection B
 A or B = A+B = A∨B = A union B
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 40
More Logic Operations

 XOR and XNOR


XOR XNOR
A B A⊕B A B (A⊕B)’
0 0 0 0 0 1
0 1 1 0 1 0
1 0 1 1 0 0
1 1 0 1 1 1

‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 41


Logical Operations Example
 AND
11000101
 useful for clearing bits
AND 00001111
 AND with zero = 0
 AND with one = no change 00000101
 OR
 useful for setting bits
11000101
 OR with zero = no change OR 00001111
 OR with one = 1 11001111
 NOT
 unary operation -- one argument NOT 11000101
 flips every bit 00111010
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 42
Text Representation
 ASCII: Maps 128 characters to 7-bit code.
 both printable and non-printable (ESC, DEL, …) characters
00 nul 10 dle 20 sp 30 0 40 @ 50 P 60 ` 70 p
01 soh 11 dc1 21 ! 31 1 41 A 51 Q 61 a 71 q
02 stx 12 dc2 22 " 32 2 42 B 52 R 62 b 72 r
03 etx 13 dc3 23 # 33 3 43 C 53 S 63 c 73 s
04 eot 14 dc4 24 $ 34 4 44 D 54 T 64 d 74 t
05 enq 15 nak 25 % 35 5 45 E 55 U 65 e 75 u
06 ack 16 syn 26 & 36 6 46 F 56 V 66 f 76 v
07 bel 17 etb 27 ' 37 7 47 G 57 W 67 g 77 w
08 bs 18 can 28 ( 38 8 48 H 58 X 68 h 78 x
09 ht 19 em 29 ) 39 9 49 I 59 Y 69 i 79 y
0a nl 1a sub 2a * 3a : 4a J 5a Z 6a j 7a z
0b vt 1b esc 2b + 3b ; 4b K 5b [ 6b k 7b {
0c np 1c fs 2c , 3c < 4c L 5c \ 6c l 7c |
0d cr 1d gs 2d - 3d = 4d M 5d ] 6d m 7d }
0e so 1e rs 2e . 3e > 4e N 5e ^ 6e n 7e ~
0f si 1f us 2f / 3f ? 4f O 5f _ 6f o 7f del
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 43
Other Data Types
 Text strings
 sequence of characters, terminated with NULL (0)
 typically, no hardware support
 Image
 array of pixels
 monochrome: one bit (1/0 = black/white)
 color: red, green, blue (RGB) components (e.g., 8 bits each)

 other properties: transparency

 Sound
 sequence of fixed-point numbers
‫مبانی کامپیوتر و برنامه سازی‬ 84 ‫ پاييز‬, 44

You might also like