26.6 C
Jaipur
Tuesday, October 19, 2021

Shift Operators in C

Must read

In general, you should not need to worry about operations somewhere at bit level as a developer. You can conceptualize in terms of bytes, int, and doubles, and even higher-level data. However, there are instances when you would like to zoom in on a single piece. The bits in an integer variable are shifted by a defined number of positions using two shift operators. The “>>” operator changes bits to the right, while the operator changes bits left. In general, employing the left and right shift operators instead of computing and then multiplication by a power of two will result in substantially faster code.

As a result, we can divide the bitwise shift operator into two major categories:

  • Left-shift operator
  • Right-shift operator

Left shift operator Example

In C, it is a bitwise shift operator that works with bits. It is a binary operator, which means it works with two operands. It’s used to move the bits of a value to the left by adding zeros to the empty spaces left after shifting. By the number of places specified by the second operand, the first operand bits are moved to the left.

Syntax

“Nameofvariable”<<TotalpositionNumbers”

There can be two values in the above-displayed syntax; the first is an integer variable on which we could use the left shift operator. This variable’s name can be anything the user wants it to be. The second value is an integer that defines how many places the bits should be moved to the left.

The outcome of the left shift operation will be unknown if the first operand is negative. Likewise, if somehow the second operand’s value is negative or more than or equal to the number of bits during the first operand, the outcome of the left shift operation is undefined. Whereas if the second operand’s value is bigger than the integer’s size, the outcome of the left shift operation would also be undefined.

We are implementing all these examples on Ubuntu 20.04 Linux operating system. Open the terminal window of the Ubuntu 20.04 Linux operating system and execute the following appended instruction in the shell to create a file titled “leftshift.c”. The file extension is .c, which depicts the file is for the c programming language.

After creation, the file will be opened in the GNU editor. Execute the listed below programming code.

In this code, we have assigned the values to “a” and “b” as “2” and “9” and applied the left shift operator on both of them. We have printed the result by utilizing the printf() function. The whole code is written in the main function. After saving this file, write the listed below commands for the execution and compilation of the above-attached code with the help of the GCC compiler.

$ gcc leftshift.c

$ ./a.out

Upon execution, the output is shown, as you can check in the attached image.

Right-shift operator Example

Two numbers are used as operands in the Right Shift (>>) operation. The second operand determines whether or whether the first operand should right shift the bits, as well as the number of places that should be shifted. In other words, 0 is used unless the number is positive, and 1 is used when the number is negative.

Syntax

“Nameofvariable”>>”TotalpositionNumbers”

There can be two values in the above line; the first is an integer variable to use the right shift operator. This variable’s name can be anything the user wants it to be. The second value is an integer that defines how many places the bits should be shifted to the right.

We are implementing all these examples on Ubuntu 20.04 Linux operating system. Open the terminal window of the Ubuntu 20.04 Linux operating system and execute the following appended instruction in the shell to create a file titled “rshift.c”. The file extension is .c, which depicts the file is for the c programming language.

After creation, the file will be opened in the GNU editor. Execute the listed below programming code.

In this code, we assigned the values to a and b as “5” and “9” and applied the left shift operator on both. We have printed the result by utilizing the printf() function. The whole code is written in the main function. After saving this file, write the listed below commands for the execution and compilation of the above-attached code with the help of the GCC compiler.

Upon execution, the output is shown, as you can check in the attached image.

Conclusion

Bitwise operators are a set of special operators given by the ‘C’ programming language. They are employed in programming at the bit level. In C /, we learned how to utilize the left and right shift operators, which are important for bit shifting operations on unsigned numbers. When the shift operators are combined, the data from the integer expression can be extracted. I believe you will easily use shift operators with the assistance of this thorough guide.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article