hGap >> = 5;
hGap >>= 5
Page 1 of 14 Replies  273 Views  Last Post: 13 February 2013  01:57 AM
#1
hGap >>= 5
Posted 12 February 2013  11:05 PM
Hello, I encountered the following statement in a sample program. Could you please let me know what it means? Thank you.
Replies To: hGap >>= 5
#2
Re: hGap >>= 5
Posted 12 February 2013  11:23 PM
Can you please post the part of the code where you found the following statement?
regards,
Raghav
regards,
Raghav
#3
Re: hGap >>= 5
Posted 12 February 2013  11:57 PM
>>= is what's called an "augmented assignment" operator.
The >> refers to a bit shift to the right. The 5 specifies how far to shift it (5 bits in this case), and the = specifies that the result will be assigned to the variable. So it's a shorthand way to write:
variable = variable >> 5;
The >> refers to a bit shift to the right. The 5 specifies how far to shift it (5 bits in this case), and the = specifies that the result will be assigned to the variable. So it's a shorthand way to write:
variable = variable >> 5;
This post has been edited by Adak: 12 February 2013  11:58 PM
#4
Re: hGap >>= 5
Posted 13 February 2013  01:29 AM
Adak is correct, it is a bit shift right than an assignment. You usually come across these statements when bit masks or bit wise logic is involved, or when someone is attempting to optimize a multiplication or division.
i.e.
These types of optimizations are usually the unnecessary, however they can be useful in embedded programming when you need to efficiently utilize clock cycles.
i.e.
x >>= 1 //divide by 2 x >>= 2 //divide by 4 x >>= 3 //divide by 8 x >>= 4 //divide by 16 x <<= 1 //multiply by 2 x <<= 2 //multiply by 4 x <<= 3 //multiply by 8 x <<= 4 //multiply by 16
These types of optimizations are usually the unnecessary, however they can be useful in embedded programming when you need to efficiently utilize clock cycles.
This post has been edited by jjl: 13 February 2013  01:33 AM
#5
Re: hGap >>= 5
Posted 13 February 2013  01:57 AM
Well, the explanation of how that takes place is the shifting of the bits of the particular number in binary.
Consider the number 8...binary equivalent is 1000
x=8; //1000
x >>= 2; would mean shifting the bits by 2 places to the right
So, final answer will be 0010 that is 2 in decimal equivalent.
regards,
Raghav
Consider the number 8...binary equivalent is 1000
x=8; //1000
x >>= 2; would mean shifting the bits by 2 places to the right
1000 //original number 0100 //shifting bits 1 place to the right 0010 //shifting bits 2 places to the right
So, final answer will be 0010 that is 2 in decimal equivalent.
regards,
Raghav
This post has been edited by raghav.naganathan: 13 February 2013  01:58 AM
Page 1 of 1
