1

When using the spotlight's calculator function, I noticed that & can be used as an operator. For example:

5 & 4 = 4
5 & 3 = 1
5 & 2 = 0
5 & 1 = 1

My question is what is this operator?

1

1 Answer 1

2

It’s a bitwise AND operator. Looking at your first example: 5 & 4 = 1. 101 (5 decimal) 100 (4 decimal) Answer has a 1 only in positions that have a 1 in both operands, which is just the most significant bit in this case: 100 (4 decimal)

4
  • 1
    So why is 1 & -1 = 0 and 1 & -2 = 0 also?
    – roydbt
    Commented Jul 3, 2023 at 12:42
  • 1
    @roydbt It's because of Two's complement representation: In it -1 is all 1s and -2 is all 1s except for the last digit.
    – cachius
    Commented Jul 3, 2023 at 17:32
  • @cachius So why isn’t the answer for the first one 1?
    – roydbt
    Commented Jul 4, 2023 at 18:34
  • Good catch! I have no idea. You can ask a new followup question.
    – cachius
    Commented Jul 6, 2023 at 8:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .