Counting/ Numbering Systems plus Integer and floating point representation in memory(Demo, not complete yet)
Numbering Systems
intro
In mathemitics, a "base" ora "radix" is the number ofdifferent digits or combin-ation of digits and lettersthat a system of countinguses to represent numbers.
- Base 10(D e ci ma l)-Represent any number using 10 Digits [0-9]
- Base 2(B i n a r y)-Represent any number using 2 Digits [0-1]
- Base 8( O c t a l )-Represent any number using 8 Digits [0-7]
- Base 16(Hexadecimal)-Represent any number using 10 Digits and 6 characters [0-9,A,B,C,D,E,F]
decimal system
0 1 2 3 4 5 6 7 8 9
10 is 1 and 0
each numbering system has a base
the decimal's base is 10
what we need to say about the base is that all numbers in any numbering system are less than the base
512 is decimal 5<10, 1<10, 2<10, each digit is less than 10-the base-
_______________________________
ones tens hundreds
1 10 100
' ' '
' ' '
'
10 ^0=1 10 ^1=10 10 ^2=100
any number in the decimmal system can be multiplied
Decimal(multiply)
(512)10 ->
2*10^0
+1*10^1
+5*10^2
=
2*1
+1*10
+5*100
=
2+10+500
=512
Binary:
important:
- Binary->Decimal
- Decimal->Binary
Binary base is 2
0,1,10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1111,....
Adding in Binary:
0 +1= 1
1 +1= 10
_________
10 +1= 11
11 +1= 100
100 +1= 101
101 +1= 110
110 +1= 111
111 +1=1000
1000+1=1001
1001+1=1010
1010+1=1011
1011+1=1100
1100+1=1101
1101+1=1110
1110+1=1111
How to convert a number into a negative number?
Using 1's compliment and 2's compliment:
1001
=>1's C
0110
=>2" C
0110+1
=0111 this is a negative number
:9
(-2)10=?()2
2: 0010
-2:
1'sC
1101
2'sC
1101+1
=1110
(-1)10=>(1110)2
(1)110 //This is a sign number.
This method works with numbers less than 10, if:
//1 : negative
//0 :positive
5:0101
-2:1110
5+-2
0101
+
1110
_______
-1-0011
it should be 4 bits
so we discard the first 1/ the first digit
result: 0011
(0011)2=>(3)10
Another example:
2-5
2=0010
-5:
5=0101
1's:
1010
2's
1011
------
2+-5
0010
+
1011
_______
1101
1:negative
101:3
::::::
-3
(1101)2=>(-3)10
2'sC of a 2's C returns the postive of a number
1101 is 2'sc of 5
1101's 1's C
0010
2sC:
0011: (3)10
_______________________________________
Hexadecimal
Hexadecimal base is 16
0,1,2,3,4,5,6,7,8,9,A ,B ,C ,D ,E ,F
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
2^4=16
each digit in hexadecimal can be written as 4 digits in binary
eg:
_______________________________
Decimal Hexa Binary
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
10 A 1010
11 B 1011
12 C 1100
13 D 1101
14 E 1110
15 F 1111
- (1 1 0 1 1 0 1 1)2 -> (1*2^0+1*2^1+0*2^3+1*2^4+1*2^5+0*2^6+1*2^7+1*2^8)10
128 64 32 16 8 4 2 1
scratch zeros:
128+64+16+8+2+1=219
- (0111)2?=(0111)10 : f, we need to make sure that both numbers are on the same numbering system.
The base is different so is the numbering system
after conversion:
is 7=111?
False, 7 is < 111
- (1001)2<(1101)2
9<13 true
1101:
this is a high significant This is a low significant
. .
. .
. .
1 1 0 1
INTEGER REPRESENTATION
8-bit representation:
13-7
13:0000 -1101-
7: 0000 0111
-7:direct conversion:
eg:
0100
start from right
0 keep it
0 keep it
1 keep it and flip every one after it
0 becomes 1
so:
7: 0000 0111
-7:1111 1001
13: 0000 1101
+
-7: 1111 1001
_______________
-1-0000 0110
//1 is a ninth bit so we discard it
0000 0110
scratch all 0's
4 2 0
4+2=6
________________________________
computer has 3 main resources:
1. I/O, HARDDISK, SSD, GRAPHICS CARD, ETC
2.RAM-Memory- stores temporary values for the cpu to deal with
3.CPU defines how mauch in parallel process the cpu can do in arithmatic and logical operations
between cpu and Ram there are wires, called busses, each bus can move one or 0 (5v or 0volt)
the number of busses and how much is dedicated for float numbers, integers, characters, etc, is dedicated to how much space these ones (floats,...) needs
the size of each data is depending on the busses between the cpu and ram and which is dedicated to which
In order to build an efficient website, we need to care about the space, memory utilization and time.
HOW MEMORIEY STORES DATA?
How integers are stored in memory?
it's different how numbers are stored in computers, but all computers work the same way
all integers are stored as 16bits:2bytes
all long integer type stored as 32bits : 4bytes
16 bits, the largest number is:
1111 1111 1111 1111
(2^16)-1 :65,535
why -1? started from 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 0
16:65535
how the bits are stored?
(17)10 ->(10001)2=>(-0-000/0000/0001/000-1-)2//the first to the left is the highest significant bit, the first to the right is the lowest significant bit
0011
|00|
|11|
| |// These represent the memory
stored in a stack
(-17):10001: 1111/1111/1110/1111
F / F /E /F
|EE|//highest significant
|FF|//lowest significant
each row in memory can contain 1 byte only which is 8 bits
1 byte is 2 digits in hexadecimal
How floating point, characters are stored in the computer?
floating point representation:
all numbers have a fraction:
42.125
.125 is a fraction
(42.125)10=>()2
1.convert to
binary
4 2 . 1
2 5
2
0 . -1 -2 -3
42 = 32+10
32+8+2
32 16 8 4 2 0
1 0 1 0 1 0
101010 :101010
0.125*2=0.25
0.25*2=0.5
0.5*2=1.0
:
(101010.001)2
2.get the power
1200.2=1.2002*10^3
100.1 = 1.001*2^2
it's base to the power of number of
digits shifter after the point
1.01010001*2^5=>5+127
127 is a
constant=132
convert 132 to a number
(10000100)2
3.put the number
into 32 bits
32bits
divided into 3 parts:
1.one
digit represent the sign
2.second
8 bits are the exponent(the power that was added to 127)
3.23bits left
the mantissa part
_
________ _______________________
|_| |________|
|_______________________|
0
10000100 01010001000000000000000
0
100/0010/0
010/1000/1000/0000/0000/0000
4 2 2
8 8 0
0 0
__
|00|
low
__
|80|
__
|28|
__
|42|
high
character representation:
Unicode 8
Unicode 16
ascii
Unicode 256
ASCII is the famous one: American
Table
A->(65)10
a->(97)100
0->(48)10
hi
a 97
b 98
c 99
d 100
e 101
f 102
g 103
h 104 : 01101000 -0 at the left was
added for no reason just to have 8 digits-
i 105 : 01101001
h 104 : 01101000 |68|
i 105 : 01101001 |69|
68 and
69 will be stored in memory
var x=8;
var y=42.5;
var w = 'b';
var arr = [3,4];
on declaring variables we are storing
them in the memory, we load it into memory
assume we have a big memory
for each index -row- in the RAM, it
has a unique address
var x: load variable x into memory
x=8
0000 0000 0000 1000
0
0 0 8
___
|0 8|
___
|0 0|
42.5:
|00
|00
|80
|28
|42
w=8:
01000010
___
|4 2|
arr
will occupy spaces in the memory equal
to the array length and are all next each other,stcked over each other
0011: 3
0100: 4
|03|
|00|
|04|
|00|
0300 is the
first index
when we have an equation or if
statement or so on.. what happens?
x=arr[0]+1
there are 3 units in cpu
arithmetic
logical
registers
1. get
the value of arr[0]
store it in registers
2.arith.unita:
dd 1
3. store
it back in the memory 'at the same place where x was?'
Swap x,y values without a temp
variable.
x=x+y;
y=x-y;
x=x-y;
or
x=x*y;
y=x/y;
x=x/y;
For More:
Comments
Post a Comment