• C

Use of Operator &= in C

 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) {

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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..

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
>>the difference being it works on the binary representation of the number
 actually no difference :P
cbeginnerAuthor Commented:
Thanks for excellent explanation
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.