How to Write a Program That Right-Shifts an Integer Variable 4 Bits

The most basic unit of information that programmers can access is the byte, a sequence of eight binary numbers called bits. Although programmers rarely concern themselves with operations at the bit level, certain applications, such as some forms of encryption, require manipulating bytes by the bit. Shifting an integer variable to the right has an affect similar to dividing the integer by the corresponding power of two. Learning to write a program that right-shifts an integer variable by four bits allows you greater control over your code.

Instructions

    • 1

      Define the integer variable. Use the commands and syntax of your preferred coding language to identify the variable on which you wish to perform the right shift. For example, in Java the variable "a" can be defined by "int a = 54."

    • 2

      Perform the right shift. Follow the syntax for your coding language to perform the right shift on the variable. Specify the number of bits to shift to the right as four. For example, in Java the code "a = a >> 4" redefines the variable "a" from step 1 as 54 shifted to the right four bits.

    • 3

      Print the resultant integer variable using the commands and syntax of your coding language. For example, in Java the code "System.out.printLn(a = a>>4)" displays the variable "a" after shifting it four bits to the right. In this example, 54 shifted to the right four bits equals 13.

Learnify Hub © www.0685.com All Rights Reserved