>1);//4// printf(&"/> >1);//4// printf(&">

Home  >  Article  >  Backend Development  >  What is the displacement operation in C language?

What is the displacement operation in C language?

王林
王林forward
2023-09-06 11:13:031110browse

Question

What is a simple program that uses C language to display the left shift, right shift, and complement of a number?

Solution

Left shift

If the value of a variable is shifted to the left once, its value will be doubled.

For example, a = 10, then a

What is the displacement operation in C language?

Shift right

If the value of a variable is shifted right once , then its value becomes half of its original value.

For example, a = 10, then a>>1 = 5

What is the displacement operation in C language?

Example

The following is C for shift operation Program-

Live Demonstration

#include<stdio.h>
main (){
   int a=9;
   printf("Rightshift of a = %d</p><p>",a>>1);//4//
   printf("Leftshift of a = %d</p><p>",a<<1);//18//
   printf("Compliment of a = %d</p><p>",~a);//-[9+1]//
   printf("Rightshift by 2 of a = %d</p><p>",a>>2);//2//
   printf("Leftshift by 2 of a = %d</p><p>",a<<2);//36//
}

Output

When the above program is executed, the following results will be produced-

Rightshift of a = 4
Leftshift of a = 18
Compliment of a = -10
Rightshift by 2 of a = 2
Leftshift by 2 of a = 36

The above is the detailed content of What is the displacement operation in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete