What are bitwise shift operators?
What are bitwise shift operators?The bitwise operators are used for shifting the bits of the first operand left or right. The number of shifts is specified by the second operator.
Expression << or >> number of shifts
Ex:
number << 3;/* number is an operand - shifts 3 bits towards left*/ number >> 2; /* number is an operand – shifts 2 bits towards right*/
The variable number must be an integer value.
For leftward shifts, the right bits those are vacated are set to 0. For rightward shifts, the left bits those are vacated are filled with 0’s based on the type of the first operand after conversion.
If the value of ‘number’ is 5, the first statement in the above example results 40 and stored in the variable ‘number’.
If the value of ‘number’ is 5, the second statement in the above example results 0 (zero) and stored in the variable ‘number’.
<< - Bitwise Left-Shift Bitwise Left-Shift is useful when to want to MULTIPLY an integer (not floating point numbers) by a power of 2. Expression: a << b This expression returns the value of a multiplied by 2 to the power of b.
>> - Bitwise Right-Shift Bitwise Right-Shift does the opposite, and takes away bits on the right. Expression: a >> b This expression returns the value of a divided by 2 to the power of b.
|
Explain the use of bit fieild. Packing of data in a structured format is allowed by using bit fields. When the memory is a premium, bit fields are extremely useful. For example:
Define the scope of static variables. The scope of a static variable is local to the block in which the variable is defined. However, the value of the static variable persists between two function calls.
Purpose of "register" keywordThe register keyword tells the compiler to store the variable onto the CPU register if space on the register is available.....