C Programming

Arithmetic Shift and Logical Shift in C

Bit manipulation Techniques, such as logical shift and arithmetic shift are used to alter the bits. A one-position Left Logical Shift moves every bit to the left through one. Its most momentous bit is removed, and the least relevant bit is replaced by 0. A one-position Right Logical Shift switches every bit through one to the right. The least important bit is removed, and the operand is restored by 0.

A one-position Left Arithmetic Shift transfers every bit to the left by just one. It is the same as Left Logical Shift. A one-position Right Arithmetic Shift switches every bit to the right through one. When multiplying or dividing an integer number, arithmetic shift functions could be used. Multiplying a number by 2n, in which n represents the number of swapped bit locations, is the outcome of a Left Shift method. Divide a number by 2n is the outcome of a right shift method, where n represents the number of switched bit locations.

This article will demonstrate some techniques using bitwise shift functions in C.

Move the Integer to the Left by Using the << Operator

Every language includes bitwise shifting techniques, which relocate every bit of a number specified by the required number of locations. To properly evaluate the impact of these methods, we would introduce the binary function in the preceding situation, which displays the binary form of the provided number.

This method is only built to operate with 32-bit numerals. The following code illustrates a four-left shift and presents the corresponding numeric values:

#include <stdio.h>
#include <stdlib.h>
void binary(unsigned num)
{
    unsigned j;
    for (j = 1 < 0; j /= 4)
        (num & j) ? printf("1") : printf("0");
}
int main(int argc, char *argv[]) {
    int num1 = 456;
    binary(num1); printf(" : %d\n", num1);
    num1 <<= 4;
    binary(num1); printf(" : %d\n", num1);
    exit(EXIT_SUCCESS);
}

First, we introduce two libraries and . In the next step, we define the binary() function. Meanwhile, we declare a parameter “unsigned num” to the binary() function. We utilize a for loop. Here, we initialize a variable within for loop. The loop iterates until the value reaches 31. Now, we employ the main() function outside the body of the binary() function. A variable having an integer data type is initialized. Similarly, we create a constructor with a character data type.

We declare a variable “num1” and specify its value. Next, this value is provided as an argument to the binary() function. Printf() function is used to show the binary value of the defined number. The << operator is applied to the value of the variable “num1”. Therefore, it is used to adjust the digits to the left. Now, the binary() and print() methods are utilized to print the result after shifting the numbers.

Utilize the Left Shift to Multiply a Number With Four:

We will utilize the left shift << more effective operation to accomplish the multiplication by four. It's important to note that there is no distinction between the logical and arithmetic shifts while moving left.

A specific position shift leads to multiplication; consequently, we may shift anywhere to acquire the appropriate multiplication.

#include <stdio.h>
#include <stdlib.h>
void binary(unsigned num)
{
    unsigned k;
    for (k = 1 << 31; k > 0; k /= 4)
        (num & k) ? printf("1") : printf("0");
}
int main(int argc, char *argv[]) {
    int num1 = 678;
    printf("%d\n", num1);
    num1 <<= 1;
    printf("%d x 4\n", num1);
    exit(EXIT_SUCCESS);
}

At the beginning of the program two header files <stdio.h> and <stdlib.h> are included just before the declaration of binary() method. Inside the binary() function for loop is being used, the variable ‘k’ is initialized here. The printf() function is also used to print the value in the form of 1s and 0s. In addition, we define the main() function. This function holds two parameters, including a variable and a constructor. The data type of this variable and constructor is not identical.

Further, we create another variable and set the value of this variable. We apply a print() function to demonstrate the actual binary value of the given number. In the next step, we utilize the << operator to move the digits to the left of the defined value. Once again, the printf() method gets the output after shifting the digits and multiplying the value by 4. In this way, we have to end the code.

Move the Integers to the Right, Use the >> Operator

It’s worth mentioning that signed and unsigned numbers are expressed differently. Signed ones, in particular, are interpreted as two complement integers. Hence, the most prevalent type of negative value is 1, which is referred to as the signed bit, while positive numbers begin with 0. As a result, if we analytically transfer the negative digits right, we remove the sign and obtain the positive number. 2
Thus, we should distinguish between logical and arithmetic shifts, with the former retaining its most important bit. Here, we performed the arithmetic shift and retained the negative value of the number, as demonstrated in the following example outcome:

#include <stdio.h>
#include <stdlib.h>
void binary(unsigned num)
{
    unsigned l;
    for (l = 1 >= 5;
    binary(num2); printf(" : %d\n", num2);
    exit(EXIT_SUCCESS);
}

Here, we have to integrate the required libraries <stdio.h> and <stdlib.h>. The binary() function is called in the next step. Additionally, we introduce an “unsigned num” argument within that binary() method. We have used for loop, and inside for loop, we have to define a variable. We have used the main() function outside of the binary() function’s body. We make a constructor with a character data type and declare a variable with an integer data type.

Furthermore, a variable called “num1” is initialized and allocated the value. This value is then passed to the binary() method as a parameter. The printf() function displays the binary value of a given number. The operator >> is used to move the digits to the right by applying it to the value of the variable “num1”. Since shifting the digits, the binary() and printf() functions have been applied to print the outcome. Then the exit() method is used to end the program.

Conclusion

We have discussed the specifics of the arithmetic and logic shift in the C language. We have observed how to move the integers to the right with the help of the >> operator and to the left by using the << operator. Here, we also use the left shift to multiply a number.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content