hdf
hdf

Reputation: 255

How does actually "&~" work?

I wonder how does it work in Unix:

user$ i=5
user$ echo $((i &~ 1)) 

4

what happens inside the parentheses?

Upvotes: 1

Views: 96

Answers (2)

hdf
hdf

Reputation: 255

ok, got it

binary 5 is 101

binary 1 is 001 -> ~1 is 110

101 & 110 -> 100 which is 4 in decimals

Upvotes: 1

anubhava
anubhava

Reputation: 785156

Both & and ~ are bitwise operators.

  • ~1 is unary bitwise negation that produces -2
  • 5 & -2 is binary bitwise AND operation that produces 4

Upvotes: 4

Related Questions