You are on page 1of 22

Memory Units

What is a bit
and byte ?
21 = 2
22 = 2*2 = 4
23 = 2*2*2 = 8
210 = 1,024

101 = 10
102 = 10*10 = 100
103 = 10*10*10 = 1,000
Memory Units
Unit of Memory Unit Name Description
Binary Digit 1 bit A bit is a binary digit, the smallest increment of data on a
(0 or 1) computer. A bit can hold only one of two values: 0 (off) or 1 (on).
At the hardware, flip-flop or latch handle a bit of information.
Hence computer memory is measured using binary number
system.

In electronics, a flip-flop or latch is a


circuit that has two stable states and can
be used to store state information.
Memory Units

Unit of Memory Unit Name Description


8 bits 1 byte 28 = 256 7 6 5 4
[(1) × 2 ] + [(1) × 2 ] + [(1) × 2 ] + [(1) × 2 ] +
[00000000] = 0 3 2 1
[(1) × 2 ] + [(1) × 2 ] + [(1) × 2 ] + [(1) × 2 ] 0

[11111111] =255
=> A byte can hold 0 to 255.

Why 8 bits? Why Not 2 or 10 bits ?


Extended Binary Coded Decimal Interchange Code (EBCDIC) is an 8-bit character encoding used mainly on IBM mainframe and IBM midrange
computer operating systems.
E B C D I C Codepage layout
—0 —1 —2 —3 —4 —5 —6 —7 —8 —9 —A —B —C —D —E —F

0−
NUL
0000
0
SOH
0001
1
STX
0002
2
ETX
0003
3
SEL

4
HT
0009
5
RNL

6
Computer Memory Units
DEL
007F
7
GE

8
SPS

9
RPT

10
VT
000B
11
FF
000C
12
CR
000D
13
SO
000E
14
SI
000F
15
DLE DC1 DC2 DC3 RES ENP NL BS POC CAN EM UBS CU1 IFS IGS IRS IUS ITB
1− 0010 0011 0012 0013 0085 0008 0018 0019 001C 001D 001E 001F
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
DS SOS FS WUS BYP INP LF ETB ESC SA SFE SM SW CSP MFA ENQ ACK BEL
2− 000A 0017 001B 0005 0006 0007
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
SYN IR PP TRN NBS EOT SBS IT RFF CU3 DC4 NAK SUB
3− 0016 0004 0014 0015 001A
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
SP RSP . < ( + |
4− 0020 00A0 002E 003C 0028 002B 007C
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
& ! $ * ) ; ¬
5− 0026 0021 0024 002A 0029 003B 00AC
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
- / ¦ , % _ > ?
6− 002D 002F 00A6 002C 0025 005F 003E 003F
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
` : # @ ' = "
7− 0060 003A 0023 0040 0027 003D 0022
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
a b c d e f g h i ±
8− 0061 0062 0063 0064 0065 0066 0067 0068 0069 00B1
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
j k l m n o p q r
9− 006A 006B 006C 006D 006E 006F 0070 0071 0072
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
~ s t u v w x y z
A− 007E 0073 0074 0075 0076 0077 0078 0079 007A
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
^ [ ]
B− 005E 005B 005D
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
{ A B C D E F G H I SHY
C− 007B 0041 0042 0043 0044 0045 0046 0047 0048 0049 00AD
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
} J K L M N O P Q R
D− 007D 004A 004B 004C 004D 004E 004F 0050 0051 0052
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
\ NSP S T U V W X Y Z
E− 005C 2007 0053 0054 0055 0056 0057 0058 0059 005A
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
0 1 2 3 4 5 6 7 8 9 EO
F− 0030 0031 0032 0033 0034 0035 0036 0037 0038 0039
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
Character Set
Ref: http://en.wikipedia.org/wiki/Character_set
Data Type Basics

Diagrammatic notation to represent variables and objects.

A variable is a chunk of memory in The object diagram is a rounded


which we hold a value that can be edge rectangle that denotes an
read or written. The variable object created at runtime and
diagram is a cubic figure that allocated in a garbage collectable
depicts three traits (name, value heap.
and type).
Type Categories
Value Type Reference Type Pointer Type

In a variable that holds a A variable that holds a pointer type is similar to


value type, the data itself reference type contains a reference type. Both
is directly contained the address of an object pointers and references
within the memory stored in the heap. actually represent
allotted to the variable. memory addresses, but
Eg: object y = new object(); References Types are
Eg: int x = 5; tracked by the garbage
collector, pointers are
not. Because of the
unwieldy nature
associated to pointers,
Note that the "value" part of they can only be used in
Note how the value 5 is the variable diagram C# within code marked
contained within the contains the start of an as unsafe.
variable itself. arrow that points to the
referred object.
Type Categories
Value Type Reference Type

Value Type is stored in Stack Reference Type is stored in Heap

Stack is an array of memory that acts as a Heap is an area where chunks of memory
last-in, first-out (LIFO) data structure. can be allocated to store certain kinds of
data in any order.
System cleans up the Stack automatically.
GC need to clean the Heap.

· Data can be added to and deleted from the top of the


stack
· Placing a data item at the top of the stack is called
pushing item to the stack
· Deleting an item from the top of the stack is called
popping the item from the stack
Type Categories

Value Type Reference Type

VB.Net VB.Net

C#.Net C#.Net

<datatype> <identifier> [= <value>] <datatype> <identifier> [= new <datatype>()]


Type Categories
Value Type (Immutable) Reference Type (Mutable)

What happens when we introduce two variables and do some copying from the
original variables.
int x = 5; object y = new object();
int a = x; object b = y;

DEMO

Variable a has a copy of the value of x. If we In the case of y and b, both variables refer to the
modify the value of one of these variables, the same object. If we alter the state of the object
other variable would remain unchanged using variable y, then the resulting changes will
(Immutable). be observable using variable b, and vice versa
(Mutable).
Boxing and Unboxing

Boxing Unboxing
Converting a Value Type to a Reference Converting a Reference Type to a Value
Type is called “Boxing”. Type is called “Unboxing”.

This operation will copy value from This operation will copy value from Heap
Stack to Heap. to Stack resulting in garbage. Hence it is
recommended to avoid this.
Boxing and Unboxing

Boxing Unboxing
Converting a Value Type to a Converting a Reference Type to a
Reference Type is called “Boxing”. Value Type is called “Unboxing”.

.Net performs implicit boxing when needed. In the below example Value Types
are passed to a method with Reference Type parameter. This will result in
implicit boxing.

DEMO
Boxing and Unboxing

Boxing Unboxing
Converting a Value Type to a Converting a Reference Type to a
Reference Type is called “Boxing”. Value Type is called “Unboxing”.

DEMO
C#.Net Data Types

.Net Primitive Data type Classification Tree


Data Types [Value Types]
VB.Net type C#.Net Type Common Type System Nominal storage Value range
allocation
Boolean Bool System.Boolean 2 bytes True or False.
Char char System.Char 2 bytes 0 through 65535 (unsigned).
Date DateTime System.DateTime 8 bytes 0:00:00 on January 1, 0001 through 11:59:59 PM
Primitive Data Types

on December 31, 9999.


Signed Numeric Data Types
SByte sbyte System.Byte 1 byte -128 through 127 (unsigned).

Short short System.Int16 2 bytes -32,768 through 32,767.

Integer int System.Int32 4 bytes -2,147,483,648 through 2,147,483,647.

Long long System.Int64 8 bytes -9,223,372,036,854,775,808 through


(long integer) 9,223,372,036,854,775,807.
Unsigned Numeric Data Types
Byte byte System.Byte 1 byte 0 through 255 (unsigned).

UShort ushort System.Int16 2 bytes 0 through 65,535.

UInteger uint System.Int32 4 bytes 0 through 4,294,967,295.

ULong ulong System.Int64 8 bytes 0 through 18,446,744,073,709,551,615.


Data Types [Value Types]
VB.Net type C#.Net Type Common Type System Nominal storage Value range
allocation

Precision Data Types


Primitive Data Types

Single float System.Single 4 bytes -3.4028235E+38 through -1.401298E-45 for


(single- negative values; 1.401298E-45 through
precision 3.4028235E+38 for positive values.
floating-point)

Double double System.Double 8 bytes -1.79769313486231570E+308 through


(double- -4.94065645841246544E-324 for negative values;
precision 4.94065645841246544E-324 through
floating-point) 1.79769313486231570E+308 for positive values.

Decimal decimal System.Decimal 16 bytes 0 through +/-


79,228,162,514,264,337,593,543,950,335 with no
decimal point;
0 through +/-7.9228162514264337593543950335
with 28 places to the right of the decimal; smallest
nonzero number is
+/-0.0000000000000000000000000001 (+/-1E-28).
Data Types [Reference Types]
VB.Net type C#.Net Type Common Type System Nominal storage Value range
allocation

Object Object 4 bytes Any type can be stored in a variable of type Object.
System.Object (class)
Primitive Data Types

This is the ultimate base class of all classes in the .NET Framework; it is the root of the
type hierarchy.

System.Object Methods
Determines whether the specified Object is equal to the current Object.
A hash code is a numeric value that is used to identify an object during
equality testing. It can also serve as an index for an object in a
collection.
Gets the Type of the current instance.
Returns a string that represents the current object.
Data Types Hierarchy
Class Type
A class is a data structure that may contain data members (constants and fields), function
members (methods, properties, events, indexers, operators, instance constructors,
destructors and static constructors), and nested types.

Class types support inheritance, a mechanism whereby a derived class can extend and
specialize a base class.

Certain predefined class types have special meaning in the C# language, as described in
the following table.
Class type Description
System.Object The ultimate base class of all other
types.
System.String The string type of the C# language.
System.ValueType The base class of all value types.
System.Enum The base class of all enum types.
System.Array The base class of all array types.
System.Delegate The base class of all delegate types.
System.Exception The base class of all exception types.
C# Data Type Alias

.Net Type C# Alias

System.Object object
Primitive Data Types

System.String string
System.Boolean bool
System.Byte byte
System.SByte sbyte
System.Int16 short
System.UInt16 ushort
System.Int32 int
System.UInt32 uint
System.Int64 long
System.UInt64 ulong
System.Single float
System.Double double
System.Decimal decimal
System.Char char
String vs StringBuilder

Value Memory Address

“Hello” 10001
“Hello World” 10002
“Hello World How” 10003
“Hello World How Are” 10004
“Hello World How Are You” 10005
Literals

A literal is a source code representation of a value.

Literal Description
boolean-literal There are two Boolean literal values: true and false.

integer-literal Integer literals are used to write values of types int, uint, long, and ulong.
Integer literals have two possible forms: decimal and hexadecimal.

real-literal Real literals are used to write values of types float, double, and decimal.

character-literal A character literal represents a single character, and usually consists of a character in
quotes, as in 'a'.

string-literal C# supports two forms of string literals:


1) regular string literals : zero or more characters enclosed in double quotes, as in
"hello“.
2) verbatim string literals: (word for word): It consists of an @ character followed by a
regular string, eg: @”hello”. In verbatim string, escape sequences are not
processed.

null-literal Used as default value for reference data type.


null

You might also like