

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the shift operations in C language?
Problem
What is the simple program to show the left, right shifts, and complement of a number by using C language?
Solution
Left Shift
If the value of a variable is left-shifted one time, then its value gets doubled.
For example, a = 10, then a<<1 = 20
Right shift
If the value of a variable is right-shifted one time, then its value becomes half the original value.
For example, a = 10, then a>>1 = 5
Example
Following is the C program for the shift operations −
#include<stdio.h> main (){ int a=9; printf("Rightshift of a = %d\n",a>>1);//4// printf("Leftshift of a = %d\n",a<<1);//18// printf("Compliment of a = %d\n",~a);//-[9+1]// printf("Rightshift by 2 of a = %d\n",a>>2);//2// printf("Leftshift by 2 of a = %d\n",a<<2);//36// }
Output
When the above program is executed, it produces the following result −
Rightshift of a = 4 Leftshift of a = 18 Compliment of a = -10 Rightshift by 2 of a = 2 Leftshift by 2 of a = 36
- Related Questions & Answers
- What are memory operations in C language?
- What are Shift Micro-operations in Computer Architectures?
- What are the different operations on files in C language?
- What are shift operators in C++?
- Explain the Character operations in C language
- What are different pointer operations and problems with pointers in C language?
- What are file operations in C#?
- What are the FTP Operations?
- What are the elements that combine to obscure the definitions of programming language operations?
- What are the predefined functions in C language?
- What are the special symbols in C language?
- What are the operations on process?
- Explain the characteristics and operations of arrays in C language
- What are the advantages of C++ Programming Language?
- What are the different searching techniques in C language?
Advertisements