404

Page not found.

Bitwise Left Shift (<<) Operator in C Programming

Bitwise Left Shift Operator in C

  1. It is denoted by <<
  2. Bit Pattern of the data can be shifted by specified number of Positions to Left
  3. When Data is Shifted Left , trailing zero’s are filled with zero.
  4. Left shift Operator is Binary Operator [Bi - two]
  5. Binary means , Operator that require two arguments

Quick Overview of Left Shift Operator

Original Number A 0000 0000 0011 1100
Left Shift 0000 0000 1111 0000
Trailing Zero’s Replaced by 0
(Shown in RED)
Direction of Movement of Data<<<<<=======Left

Syntax : Bitwise Left Shift Operator

[variable]<<[number of places]

Live Example : Bitwise Operator [Left Shift Operator]

#include<stdio.h>
int main()
{
int a = 60;
printf("\nNumber is Shifted By 1 Bit : %d",a << 1);
printf("\nNumber is Shifted By 2 Bits : %d",a << 2);
printf("\nNumber is Shifted By 3 Bits : %d",a << 3);
return(0);
}

Output :

Number is Shifted By 1 Bit  : 120
Number is Shifted By 2 Bits : 240
Number is Shifted By 3 Bits : 480

Explanation of Left Shift Binary Operator :

We know the binary representation of the 60 is as below -

0000 0000 0011 1100

Now after shifting all the bits to left towards MSB we will get following bit pattern -

0000 0000 0011 1100   = 60
<< 1
-------------------
0000 0000 0111 1000   = 120

Observations and Conclusion :

Shfting Binary 1 to Left By ‘X’ BitsDecimal ValueConverted Value
0 Bit2^01
1 Bit2^12
2 Bits2^24
3 Bits2^38
4 Bits2^416
5 Bits2^532
6 Bits2^664
7 Bits2^7128
8 Bits2^8256

What is meaning of (++*ptr) in Pointer ?

In the previous topic we have studied Precedence of Value at and Address Operator of Pointer. In this chapter we will be looking more advance form of pointer. We are looking one step forward to learn how (++*ptr) works ?

Meaning of (++*ptr) in C Programming :

Consider the Following Example :

int num = 20 , *ptr ;
ptr = &num;
printf("%d",++*ptr);

Explanation :

  1. ‘++’ and ‘*’ both have Equal Precedence
  2. Associativity is from Right to Left ( Expression evaluated from R->L)
  3. Both are Unary (Operates on single operand )
  4. So ‘ * ‘ is Performed First then ‘ ++ ‘

Re-Commanded Reference Article : Operator Precedence Table

Visual Understanding :

Calculation of Answer :

  ++*ptr     =  ++ ( *ptr )
             =  ++ ( *3058 )
             =  ++ ( 20 )
             =  21
  1. Suppose ptr variable stores address 3058.
  2. * Operator will de-reference pointer to get value stored at that address. (i.e 20)
  3. Pre-increment on 20 will perform increment first then assignment

Live Example : Meaning of ++*ptr Pointer

#include<stdio.h>
int main()
{
int n = 20 , *ptr ;
ptr = &n;
printf("%d",++*ptr);
return(0);
}

Output :

21

In the next chapter we will be learning the meaning of (*++ptr) Pointer.

What is the size of Pointer variable in C ?

In the previous chapter, we have learnt about the memory organization of the pointer variable. In this chapter we are learning to compute or find the size of pointer variable.

What is the size of Pointer variable in C ?

Pointer is the variable that stores the address of another variable.

How to calculate size of Pointer ?

Consider the following memory map before declaring the variable.In the memory comprise of blocks of 1 byte.

Whenever we declare any variable then random block of memory is chosen and value will be stored at that memory location.

Similarly whenever we declare any pointer variable then random block of memory will be used as pointer variable which can hold the address of another variable.

Calculating Size of Pointer : Tips

  1. Pointer stores the address of the Variable.
  2. Size of Pointer Variable can be evaluated by using sizeof operator
  3. Address of variable is considered as integer value.
  4. For Borland C/C++ Compiler, for storing integer value 2 bytes are required.
  5. Thus Pointer variable requires 2 bytes of memory.

Size of Integer Pointer Variable in Different IDE :

CompilerSize of IntegerSize of Integer Pointer
Borland C/C++2 Bytes2 Bytes
Turbo C/C++2 Bytes2 Bytes
Visual C++4 Bytes4 bytes

Size of the Pointer :

#include<stdio.h>
int main()
{
int num = 15;
int *ptr = NULL;
ptr = &num;
printf("Size of Pointer : %d Bytes",sizeof(ptr));
return 0;
}

Output :

Size of Pointer : 2 Bytes

Some of the Useful Programs :

NoProgram
1C Program to Calculate Size of Integer Pointer
2C Program to Calculate Size of character Pointer
3C Program to Calculate Size of float Pointer
4C Program to compute size of integer far pointer
5C Program to find Size of Pointer to Structure

Application of Array in C Programming

Application Of Array :

Array is used for different verities of applications. Array is used to store the data or values of same data type. Below are the some of the applications of array -

A. Stores Elements of Same Data Type

Array is used to store the number of elements belonging to same data type.

int arr[30];

Above array is used to store the integer numbers in an array.

arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;

Similarly if we declare the character array then it can hold only character. So in short character array can store character variables while floating array stores only floating numbers.

B. Array Used for Maintaining multiple variable names using single name

Suppose we need to store 5 roll numbers of students then without declaration of array we need to declare following -

int roll1,roll2,roll3,roll4,roll5;
  1. Now in order to get roll number of first student we need to access roll1.
  2. Guess if we need to store roll numbers of 100 students then what will be the procedure.
  3. Maintaining all the variables and remembering all these things is very difficult.

Consider the Array :

int roll[5];

So we are using array which can store multiple values and we have to remember just single variable name.

C. Array Can be Used for Sorting Elements

We can store elements to be sorted in an array and then by using different sorting technique we can sort the elements.

Different Sorting Techniques are :

  1. Bubble Sort
  2. Insertion Sort
  3. Selection Sort
  4. Bucket Sort

D. Array Can Perform Matrix Operation

Matrix operations can be performed using the array.We can use 2-D array to store the matrix.

Matrix can be multi-dimensional

E. Array Can be Used in CPU Scheduling

CPU Scheduling is generally managed by Queue. Queue can be managed and implemented using the array. Array may be allocated dynamically i.e at run time. [Animation will Explain more about Round Robin Scheduling Algorithm | Video Animation]

F. Array Can be Used in Recursive Function

When the function calls another function or the same function again then the current values are stores onto the stack and those values will be retrieve when control comes back. This is similar operation like stack.

if-else-if Statement OR else if Ladder : Tutorial on If

if-else-if Statement OR else if Ladder :

Suppose we need to specify multiple conditions then we need to use multiple if statements like this -

void main ( )
{
    int num = 10 ;
    if ( num  >  0 )
        printf ("\n Number is Positive");
    if ( num  <  0 )
        printf ("\n Number is Negative");
    if ( num  ==  0 )
        printf ("\n Number is Zero");
}

which is not a right or feasible way to write program. Instead of this above syntax we use if-else-if statement.

Consider the Following Program -

void main ( )
{
    int num = 10 ;
    if ( num  >  0 )
        printf ("\n Number is Positive");
    else if ( num  <  0 )
        printf ("\n Number is Negative");
    else 
        printf ("\n Number is Zero");
}

Explanation :

In the above program firstly condition is tested i.e number is checked with zero. If it is greater than zero then only first if statement will be executed and after the execution of if statement control will be outside the complete if-else statement.

else if ( num  <  0 )
        printf ("\n Number is Negative");

Suppose in first if condition is false then the second condition will be tested i.e if number is not positive then and then only it is tested for the negative.

else 
   printf ("\n Number is Zero");

and if number is neither positive nor negative then it will be considered as zero.

Which is faster and feasible way for Writing If ?

PointExample 1Example 2
No of If Statements32
No of times Condition Tested (Positive No.)31
No of times Condition Tested (Negative No.)32
No of times Condition Tested (Zero No.)32

Flowchart for If-Else Ladder :

C Programming MCQ : Conditional Operator Multiple Choice Question [Set 1]

C Programming MCQ : Conditional Operator Multiple Choice Question [Set 2]

Features of C Programming : (MCQ) Multiple Choice Questions

C Programming [MCQ] : Applications of C Multiple Choice Questions

C Programming [MCQ] : Compiler Basic Multiple Choice Questions