Session 3 and 4
Session 3 and 4
▪ Solution:
▪ Load R3, A
▪ Load R4, B
▪ Add R5, R3, R4
▪ Store R5, C
2
▪ Number representation
▪ Decimal number system:
▪ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
▪ Each digit has a position value in terms of powers of 10
▪ 123= 1 * 102 + 2 * 101 + 3 * 100
▪ Binary number system
▪ 0, 1
▪ Each digit has a position value in terms of powers of 2
▪ 101= 1 * 22 + 0 * 21 + 1 * 20
▪ n-bit vector B = bn−1 . . . b1b0,
▪ where bi = 0 or 1 for 0 ≤ i ≤ n − 1
▪ unsigned integer value V(B) in the range 0 to 2n − 1, where
▪ V(B) = bn−1 × 2n−1 +· · ·+b1 × 21 + b0 × 20
3
▪ Unsigned integer
▪ If the integers are represented using 4 bit
▪ 0(10) in binary?
0000
▪ 15(10) in binary?
1111 = 1 * 23 + 1 * 22 + 1 * 21 + 1 * 20
▪ Signed integer
4
▪ Unsigned integer
▪ If the integers are represented using 4 bit
▪ 0(10) in binary?
0000
▪ 15(10) in binary?
1111 = 1 * 23 + 1 * 22 + 1 * 21 + 1 * 20
▪ Signed integer
▪ Sign and magnitude
▪ One’s complement
▪ Two’s complement
+3 0011 1011 -3
+2 0010 1010 -2
+1 0001 1001 -1
+0 0000 1000 -0
6
▪ Negative values - complementing each bit in Positive Value b3 b2b1b0 b3 b2b1b0 Negative Value
the corresponding positive value +7 0111 1000 -7
▪ Negative to positive- complementing each bit 1001 -6
+6 0110
in the corresponding negative value
+6 0110 +5 0101 1010 -5
7
▪ 2’s-complement of an n-bit number is done by
First Value Second Value Difference
subtracting the number from 2n.
0 0
▪ How to subtract 2 numbers? 0
1
▪ 0101 – 0100 0 1
( by borrowing)
▪ 001 1 0 1
▪ 01100 – 01000 1 1 0
▪ 0100
▪ 010000 – 0101
▪ 01011
▪ 01000 – 0011
▪ ?
8
▪ Negative values - adding 1 to the 1’s- Positive Value b3 b2b1b0 b3 b2b1b0 Negative Value
complement of corresponding positive value +7 0111 1000 -8
▪ Example: +6 to -6 in a 4-bit representation 1001 -7
+6 0110
▪ 2n-1 =15 1111
+5 0101 1010 -6
+6 0110 conversion using
1001 1’s complement +4 0100 1011 -5
+1 0001 1100 -4
+3 0011
-6 1010
+2 0010 1101 -3
▪ For n-bit numbers, this operation is equivalent
1110 -2
to subtracting the number from 2n. +1 0001
1111 -1
▪ Example: +6 to -6 in a 4-bit representation +0 0000
▪ 2n 10000
+6 00110
-6 01010
▪ 0 represented as positive 9
▪ 4 bits: -8 to +7
-24-1 to +24-1-1
▪ 5 bits: -16 to +15
-25-1 to +25-1-1
▪ 6 bits: -32 to +31
-26-1 to +26-1-1
▪ n bits: -2n−1 to +2n−1 − 1
10
▪ For 4-bit numbers, the value −8 is
representable in the 2’s-complement
system but not in the other systems.
▪ Sign-and-magnitude system seems the
most natural
▪ 1’s-complement system is easily related to
this system
▪ 2’s-complement appears unusual.
▪ However, it leads to the most efficient way to
carry out addition and subtraction operations.
▪ It is the one most often used system in
modern computers.
11
▪ #include <iostream>
using namespace std;
int main() {
unsigned short x=65535, y=65537;
cout<<x<<" "<<y<<endl;
return 0;
}
▪ x=65535, y=1
▪ unsigned short: 16 bits
▪ 0 to 65535 (0 to 2n-1)
▪ If the value is out of range, it is divided by largest_number+1 of that datatype, and
only the remainder kept.
▪ Here, 65537 % 65536= 1
▪ Any number bigger than the largest number, “wraps around” the largest number in
that type.
12
▪ #include <iostream>
using namespace std;
int main() {
unsigned short x=0, y=-1;
cout<<x<<" "<<y<<endl;
return 0;
}
▪ x=0, y= 65535
▪ wraps around to the top of the range.
13
▪ 7 (0111) + 5 (0101) = ?
▪ From 7 move 5 units in clockwise direction
▪ 12
▪ 9 + 14=?
▪ From 9 (1001) move 14 units in clockwise direction
▪ 7
14
▪ 2-bit addition:
15
▪ #include <iostream>
using namespace std;
int main(){
unsigned int x=1, y=2;
cout<<x-y;
return 0;
}
▪ 4294967295
▪ This occurs due to -1 wrapping around to a number close to the top of the range of a 4-byte
integer
▪ 232-1➔ 4294967296-1
▪ 32 bits = 4 bytes (size of int)
17