C
C
cybernoob2018-01-30 11:33:32
PHP
cybernoob, 2018-01-30 11:33:32

PHP bitwise operations. What is exclusive or?

There is a doc php.net/manual/ru/language.operators.bitwise.php where next to the ^ operator it says:
Only those bits are set that are set either only in $a or only in $b, but not in both at the same time.
$sum = 1^1; // 0
$sum = 1^2; // 3
$sum = 4 ^ 5; // 1
For example 1 and 2.
1 when converted to binary = 1
2 when converted to binary = 10
Where does 3 come from?
The same:
4 = 100
5 = 101
Answer 1. Well, in 4k there is such a bit ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Bondar, 2018-01-30
@cybernoob

As a result, we get only those bits that differ between A and B

00000001 = 1
00000001 = 1
--------
00000000 = 0

00000001 = 1
00000010 = 2
--------
00000011 = 3

00000100 = 4
00000101 = 5
--------
00000001 = 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question