4 Replies - 310 Views - Last Post: 13 February 2013 - 01:57 AM Rate Topic: -----

#1 davemot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-February 13

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.

 hGap >> = 5; 

Is This A Good Question/Topic? 0
  • +

Replies To: hGap >>= 5

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

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
Was This Post Helpful? 0
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

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;

This post has been edited by Adak: 12 February 2013 - 11:58 PM

Was This Post Helpful? 2
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

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.

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

Was This Post Helpful? 1
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

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

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1