Home > Backend Development > C++ > body text

What is the displacement operation in C language?

王林
Release: 2023-09-06 11:13:03
forward
1160 people have browsed it

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//
}
Copy after login

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
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template