Basic Refresher, 1D Array
Basic Refresher, 1D Array
Number System
○ In the mathematics there are mainly 4 type of number system
○ Decimal (Base 10, 0 to 9)
○ Binary (Base 2, 0 and 1)
○ Octal (Base 8, 0 to 7)
○ Hexadecimal (Base 16, 0 to 16)
What is the purpose of learning here? When we type any letter or word, the
computer translates them into numbers since machines can only understand the
binary numbers. To understand this conversion better we will explore the number
system here.
Decimal to binary
125 to binary
125 ⇒ 1011101
Binary to decimal
==> 1 * 2^0 + 0 * 2^1 + 1 * 2^2 + 1 * 2^3 + 1 * 2^4 + 1*2^5 + 1*2^6 + 0 *2^7
==> 1 + 0 + 4 + 8 + 16 + 32 + 64 + 0
==> 125
Decimal to octal
212 to octal ===> 0324
Decimal to hexadecimal
472 to hexadecimal ==> 1D8
Hexadecimal to binary :
● The maximum value of hexa is F which is 15, so to represent 15 4 bits are required.
● Therefore whenever hexa is to be converted to binary then each digit
should be converted to 4bit binary value like below:
Octal to binary :
● The maximum value of octal is 7, so to represent 7, 3 bits are required.
● Therefore whenever octal is to be converted to binary then each digit should
be converted to 3bit binary value like below:
Octal to hexadecimal:
● To convert octal number to hexadecimal
○ Convert octal number to binary
○ group binary number into 4 bits each
○ take the equivalent 4-bit value of hexa
○ E.g., 324 octal to hexa
○ 324 in binary is 011 010 100
○ Group above binary to 4 bits then it will be:
Hexadecimal to octal:
e.g, 1D8 in octal
Data Representation
● In a computer system different data is represented in a specific way. Here, we will
try to explore different ways of representing data in computer
● First, is Bit representation, it's the only language which is understood by the
machine.
○ Bit is derived from the word binary digit.
○ It has 2 possible states: true or false or 0 or 1.
● Byte is a collection of 8-bit.
○ its a smallest unit of information in the computer memory.
● Character is a 1byte integer which is represented differently in different languages
with the help of character code like, ASCII, EBCD, UTF etc.
○ In C, character is represented with the ASCII code.
○ Characters are represented with ‘ ‘ and a character will be 1byte
information
○ For example if you use any character internally these are represented as
follows:
1's complement + 1
0000 0000 0000 0000 0000 0000 0000 1101
1.Xe+/-y
0 1 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
● If negative only is given then only sign bit will be 1 other rest of the steps are same
as previous
● If the value is double then step 1 and 2 remain the same. In step 3 the standard
bias number will be 1023 for double.
step 3: exponent
127 + 0 == 127==> 0111 1111
0 0 1 1 1 1 1 1 1 1 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
For double
--> All the steps are same only change is representation of exponent and mantissa and
standard bias number is 1023
1.010e
X - 0100000000000000000000000000000000000000000000
sign = 0
3. exponent -->
1023 ==> 1023 + 1 ==> 1024 ⇒ 1000000000
0 01000000000 010000000000000000000000000000000000
Data types
● Data Type specifies which type of value a variable has and how much memory can
be allocated to each.
● In C data types are divided into primitive/basic and non primitive(user defined) data
types.
● Here, MSB(Most Significant Bit is dedicated for sign bit), if 0 its a positive number,
● So,
● Example,
○ char ch = 133;
0111 1010
0000 0001
● Example,
● 0000 1101
1111 0010
00000001
1111 0011
● When u try to print the value then the output will be positive equivalent of 1111
● Now, variables can be declared with both the signedness and size modifiers like,
Single Iteration:
● If and its family: If is a single iterative conditional construct which will execute the
statements only once.
syntax:
if(expression)
{
statements;
}
● Examples:
int num = 10;
if(num == 10)
{
printf(“num is equal to 10\n”);\
}
switch conditional construct
● Switch is also a single iterative condition construct which will execute the
statements once which is matching with the case label.
syntax:
switch(expression)
case label:
statements;
break;
case label:
statements;
break;
default:
statements
case 10:
printf(“10 is selected\n”);
case 20:
printf(“20 is selected\n”);
default:
printf(“None is matching\n”);
● In the above example if the expression is having 10, then all the 3 printf is
executed.
switch vs if else
● consider the below scenario where x = 10, then execution time for if is
if (x == 100) 1 cycle
{
} 2 cycle 200nsec * 4 == 800nsec
else if(x == 90)
{
} 3 cycle 4th cycle
else if(x == 80)
{
}
else
{
}
case 100:
.
. .
case 90: default:
. 1cycle == 200nsec
.
case 80:
.
Multi Iteration Conditional construct:
● While loop is known as entry controlled loop where statements are executed
repeatedly until the condition is true.
● syntax:
while(expression)
{
statement;
}
● Example:
int iter = 0;
while(iter < 5)
{
printf(“Looped %d times\n”,iter);
iter++;
}
● In the above loop, iter is 0 so the condition is true it will enter the loop.
○ prints, looped 0 times then increments the iter = 1
○ continues till iter = 5
○ Once the condition is false comes out of the loop.
Interpretation:
1. whileÀàint main() int main()
{
{
int iter;
int iter;
iter = 0;
iter = 0;
while (iter < 5)
while (iter < 5)
printf("Looped %d times\n", iter);
iter++; {
printf("Looped %d times\n", iter);
return 0; }
} iter++;
return 0;
}
2.
compiler view
do…while loop
While vs do..while
For loop:
● For loop is also like while loop, which will execute the statements repeatedly until the condition
is true.
● For loop syntax:
for(initialisation; condition ; post evaluation)
{
statements;
}
● For loop will be executed as stated below:
1. Initialization
2. Condition - if true goto step 3 else goto step 6
3. Statements
4. Post evaluation expression
5. Goto step 2
6. Exit
1.
int main() int main()
{ {
int iter = 0;
int iter = 0; for (iter = 0 ; iter < 10; iter++ )
{
for (iter = 0 ; iter < 10; iter++ ); ;
{ }
printf("Looped %d times\n", iter); {
} printf("Looped %d times\n", iter);
return 0; }
} return 0;
}
2.
3.
continue statement
--> only used in multi iterative conditional construct --> do..while,while,for
int iter = 5;
goto lab1;
printf("Hello\n");
lab1:
printf("World\n");
Unary Operators
--> used with one operand
--> highest precedence in the table
Type Conversion
2. OR Operator --> ||
Examples:
1. int a = 10,b=20;
a && b ==> 10 && 20 ==> true && true ==> true ==> 1 1
if (++num1 || num2++)
{
//statement
}
Here, ++num1 ⇒ ++1 is true value so second is not evaluated
● Similarly if(num2++ && num1++)
Here, the first expression is false so the second is not evaluated.
Examples:
1.
2.
Compound Statements:
Bitwise Operators
● Bitwise operators work on bits of the given values.
● Bitwise operators are used whenever a particular bit/s has to be set, clear or
toggle.
Examples:
Shift Operators
1. Left Shift --> bits will be shifted to left side depending on the number of shift
2. right shift --> bits will be shifted to right side
2.
❖ After shifting, if the result is not within the range of datatype then undefined behavior
e.g, signed char ch = 96;
ch << 4 ==> 1536 //the value is out of range
Here, num1=3
num1 = 6, x = 3, y = 4, z = 6
Examples:
Arrays
syntax:
datatype array_name[SIZE]; e.g,
int arr[5]; //integer array of size 5
int arr[5] = {10,20,30,40,50};