Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Use of Operator &= in C

Hi,
 Can anyone please let me know the use/meaning of operator &=

eg from a book:

#define L_FIX
#define X_BIAS 0x0010
#define REG_2 2
#define l_tone_on 1
#define addr 2

void hw_write (byte addr, word mask, word data)
{
  #ifdef L_FIX
   if ((addr == REG_2) && (l_tone_on)) {
   mask &= ~X_BIAS;
   if (mask == 0) {
  return;
  }
 }
#endif
}

Thanks
0
cbeginner
Asked:
cbeginner
  • 2
1 Solution
 
akshayxxCommented:
if u have any done electronics courses .. then u might have idea of what's the meaning of AND

like 1 AND 0 = 0
      0 AND 1 = 0
      0 AND 0 = 0
      1 AND 1 = 0

this '&' operator in C also same thing .. the difference being it works on the binary representation of the number

2 in binary will be '10'
3 will be   '11'

so 2 & 3 will be 11 & 10 = 10 = 2

and in above case when u do
a&=b; this means
a= a&b;
operate & operator on 'a' and 'b' and assign it to 'a'


& and | operators are used to set the particular bit  ( specified in the MASK ) to 1 OR 0.. and also to know whats the bit value

i hope u can now understand how it is being used in above particular case..
0
 
akshayxxCommented:
>>the difference being it works on the binary representation of the number
 actually no difference :P
0
 
cbeginnerAuthor Commented:
Thanks for excellent explanation
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now