-4
'x&=5'

What does it mean when its like this: x&=3 What does &= and what does the &= means?

Nikaido
  • 4,443
  • 5
  • 30
  • 47
arun saru
  • 1
  • 1
  • 1
  • `&` is bitwise AND. `&=` is bitwise AND + assignment. Equivalent to `x = x & 5` – rdas Oct 03 '19 at 09:00
  • 1
    I agee with the comment above. This should not be on Stack Overflow. In the future please try to find answers first before posting questions. – GBouffard Oct 03 '19 at 09:03
  • explain step wise with an example for &= operator in python? because i don't understand how did came output by using that operator . – arun saru Oct 03 '19 at 09:09
  • 1
    Possible duplicate of [Bitwise operation and usage](https://stackoverflow.com/questions/1746613/bitwise-operation-and-usage) – Nick is tired Oct 03 '19 at 09:45

1 Answers1

1

Basically, x+=y == x = x+y[*] and same for many other operators.

This means your x&=5 is the same as x = x&5.

So what's &? It's a bitwise 'and'. You can read more about bitwise operators here.

&5 basically takes 3rd and 1st lowest bits (because 5 dec == 101 bin) from whatever is your x.


Notes:

[*] The implementation isn't always the same. += on lists modifies the current list, rather than make a new list with the sum and assign it to the name. But the effect is the same.

h4z3
  • 5,265
  • 1
  • 15
  • 29