+ 1
Hello every body, I do this code
<? Php Var_dump(5&4); ?> And the result is: Int(4) I don't understand what mean that can any one explain what you mean that and thank in advance.
2 Respostas
+ 3
5 in binary is 0101
4 in binary is 0100
Let's see what will happen when you use & (and) bitwise operator on integer operands,
0101
0100 &
0100
0100 is 4 in decimal number system (your output).
I hope that you are familiar with logical & (and) operator, and that you already know how it works:
true & true, gives true
true & false, gives false
false & true, gives false
false & false, gives false
Now imagine that true is 1 and false is 0, then apply the same logic to every bit. In your example:
0101
0100 &
0100
Let's start with right-most bit
1 & 0 will give 0
0 & 0 will give 0
1 & 1 will give 1
0 & 0 will give 0
The result is 0100 which is 4 in decimal number system.
_____________________________
You can check if the number is even or odd by using & bitwise operator and number 1:
0101 5
0001 & 1 &
0001 1
if the output is equal to 1, then the number is odd
if the output is equal to 0, then the number is even.
More info:
https://www.sololearn.com/learn/4072/?ref=app
0
Think a lot! 😁