3.1 – Computer Memory and Data Representation

by subbu on August 18, 2013

Computer memory:

RAM is the primary or main memory of computer. It is the place where the text, data, instructions and intermediate results are stored while executing a program. The total memory is organized into number of bytes and each byte is again divided into 8 bits. A bit is the smallest unit in the memory, these bits are the places where the data is stored as 1’s and 0’s called binary data. In the memory every byte is identified with a number called address. It is always a positive number.

computer memory bits and bytes

While executing a program, the total memory is divided into number of segments called text, un-initialized global, initialized global, stack and heap segments. The total program is loaded into the text segment and the memory to variables is selected in the stack memory.

computer memory segmentation

Number representation in memory:

The number will not be stored as it is in the computer memory. It will be first converted into its equivalent binary and then stores into the bits of memory.

For example when we assign a number 197 to the variable x then the number 197 will first converted  into its equal binary 11000101 and then stored into the memory. Same thing happens even with a floating point number also.

Integer representation in memory

Character representation in memory:

Like numeric data even a character can’t be stored as it is, it will be first converted into its equal binary and then stores into the bits of memory. Every character on the keyboard has its equal binary value. The decimal equal to that binary value is called ASCII (American standard code for information interchange) value. Say for example equal binary value to character ‘A’ is 01000001, the decimal equal to which is 64. So the ASCII value of ‘A’ is 64. Let us see another example, say equal binary value to character ‘a’ is 01100001, the decimal equal to which is 97. So the ASCII value of ‘a’ is 97.

Example:

When a character constant ‘A’ is assigned to the char type of variable ch, then its binary equal 01000001 is stored in the memory.
The decimal equal to 01000001 is 65.
Here 65 is called the ASCII (American Standard Code for Information Interchange) value of ‘A’.

A representation in computer memory

Example:

When a character constant ‘a’ is assigned to the char type of variable ch, then its binary equal 01100001 is stored in the memory.
The decimal equal to 01100001 is 97.
Here 97 is called the ASCII (American Standard Code for Information Interchange) value of ‘a’.

a representation in memory

Conversion with printf():

The output of char type of variable depends on the formatting character we use in the printf(). Equal integer (ASCII) is printed when %d is used as the formatting character, equal character is printed when %c is used as the formatting character.

Example:

#include<stdio.h>
int main()
{
char ch;
ch='a';/* Assigns 01100001 */
printf("%c",ch);/* %c prints equal character 'a'*/
printf("\n%d",ch);/*  %d prints equal number 97*/
return 0;
}

Output:
a
97

Example:

#include<stdio.h>
int main()
{
int n;
n=65;/* Assigns 01000001 */
printf("%c",n);/* %c prints equal character ‘A’ */
printf("\n%d",n);/* %d prints equal number  65  */
return 0;
}

Output:
A
65

ASCII TABLE

ASCII Hex Symbol

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
NUL
SOH
STX
ETX
EOT
ENQ
ACK
BEL
BS
TAB
LF
VT
FF
CR
SO
SI
ASCII Hex Symbol

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
10
11
12
13
14
15
16
17
18
19
1A
1B
1C
1D
1E
1F
DLE
DC1
DC2
DC3
DC4
NAK
SYN
ETB
CAN
EM
SUB
ESC
FS
GS
RS
US
ASCII Hex Symbol

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
20
21
22
23
24
25
26
27
28
29
2A
2B
2C
2D
2E
2F
(space)
!

#
$
%
&

(
)
*
+
,

.
/
ASCII Hex Symbol

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
30
31
32
33
34
35
36
37
38
39
3A
3B
3C
3D
3E
3F
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
ASCII Hex Symbol

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
40
41
42
43
44
45
46
47
48
49
4A
4B
4C
4D
4E
4F
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
ASCII Hex Symbol

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
50
51
52
53
54
55
56
57
58
59
5A
5B
5C
5D
5E
5F
P
Q
R
S
T
U
V
W
X
Y
Z
[
\
] ^
_
ASCII Hex Symbol

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
60
61
62
63
64
65
66
67
68
69
6A
6B
6C
6D
6E
6F
`
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
ASCII Hex Symbol

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
70
71
72
73
74
75
76
77
78
79
7A
7B
7C
7D
7E
7F
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~


Specification:
Accept any four digit number and print the sum of ASCII values of all digits.

Logic:  Every digit has its ASCII value. The ASCII value of 0 is 48, 1 is 49 (48+1), 2 is 50 (48+2) …….and n is 48+n

#include<stdio.h>
int main()
{
int n,first,second,third,fourth,asc_sum;
printf("Enter any four digit integer:");
scanf("%d",&n);
fourth=n%10;
n=n/10;
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
asc_sum=(first+48)+(second+48)+(third+48)+(fourth+48);
printf("Sum of ASCII values of digits %d",asc_sum);
return 0;
}

Execution:
Enter any four digit integer:1234
Sum of ASCII values of digits 202

Previous post:

Next post: