Bitwise Left Shift operator [duplicate] - php

This question already has answers here:
What are bitwise shift (bit-shift) operators and how do they work?
(10 answers)
Closed 4 years ago.
$a = 23;
$res = $a << 4
print($res);
For the code snippet pasted above, the output is 368. How is it being calculated?
I expected 92.
a is 23
a in base 2 is 10111
so a in 8 bits is 00010111
Left shift 4 is 01110000
It is - 92
Can some body explain me ?

This is why it is returning 368 because $a << $b Shift the bits of $a $b steps to the left
23
256 128 64 32 16 8 4 2 1
X X X X 1 0 1 1 1
after $a << 4
368
256 128 64 32 16 8 4 2 1
1 0 1 1 1 0 0 0 0

Check bitwise operator manual here
Convert 23 in binary and it is 10111 and when you perform shift left bitwise operator, its value will be 101110000 and when you convert it to decimal it becomes 368.

Related

php synatax $b = (6 << 1); clarification [duplicate]

This question already has answers here:
PHP Operator <<
(5 answers)
Closed 7 years ago.
I am not clearly understand about the following code snippets.
$a = (5 << 0);
$b = (6 << 1);
echo $a|$b;
From php.net i knew that << operator use for shift left but not clear how it works and what is the uses of | operator.
Any explanation is highly appreciated.
Thank you
5 << 0 produces just 5, since no shift is done. 6 << 1 will shift the bits in 6 (110b) one to the left, which will produce 12 (1100b). So it is multiplying by two essentially.
The | operator is bitwise or, which operates on the bits of 5 (0101b) and 12 (1100b) producing 13 (1101b)
6 is '110' in binary. If you shift '110' once to the left you get '1100' which is 12
<< is the bitwise shift left operator:
00000110 is 6 in binary
6 << 1 means that each bit will be shifted 1 to the left:
00000110 (6)
becomes
00001100 (12)
so... 6 << 1 = 12
5 << 0 makes no difference as none of the bits are being shifted (5 << 0 = 5).
| is the bitwise 'or' operator:
5|12 makes:
00000101 | 00001100
This operator means if both bits are 0, the result will be 0, otherwise 1:
00000101 (5)
00001100 (12)
00001101 (13)
So 5|12 = 13
Hope you can find the solution here
Strange print behaviour in PHP?
for more information, you can check this link
Reference - What does this symbol mean in PHP?

In PHP 2<<3 returns 16 and 3 << 2 returns 12 Can some one explain this please

<?php
echo 2<<3; //Output 16
echo '---';
echo 3<<2; //Output 12
?>
Tried to find out logic. But its ends up in vain!! Can someone explain it please
The << operator is a bitwise operator. This basically means the numbers are treated as binary numbers, and the interaction is about moving bits around.
So let's have a look at the numbers and the operations:
First, 2 << 3
0b000010 // 2 in binary
0b010000 // move the bits three left, we get 16
Then 3 << 2
0b000011 // 3 in binary
0b001100 // move the bits two left, we get 12
From the manual page linked above:
Shift the bits of $a $b steps to the left (each step means "multiply by two")
So 3<<2 in effect means 3*(2^2), while 2<<3 means 2*(2^3).
The << operator is bitwise left shift.
Let's write the numbers in their binary representation
0000 0010 // 2
0000 0011 // 3
And then shift them by 3 and 2 respectively:
0001 0000 // 16
0000 1100 // 12
2 = 0b10
0b100 = 4
0b1000 = 8
0b10000 = 16
3 = 0b11
0b110 = 6
0b1100 = 12
The fist operator (<<) is the bitwise shift operator, specifically the left-shift operator. It takes the left hand argument and shifts the binary representation to the left by the number of bits specified by the right argument. Right-shift (>>) does the same but to the right. Read more about it here http://php.net/language.operators.bitwise

Pipe sign in PHP Code

I wanted to concatenate 2 variables, and by error I typed another code and I got a strange result.
This is what looks like the code :
echo 'Hello World' | 'test';
Result : |e|o World
What the pipe sign do if not concatenated ?
According to the PHP manual
"|" is a "bitwise OR". Bitwise operators allow evaluation and manipulation of specific bits within an integer.
Example Name Result
$a | $b Or (inclusive or) Bits that are set in either $a or $b are set.
Example:
$a = 9;
$b = 10;
echo $a | $b;
This would output the number 11 as follows:
1 Byte ( 8 bits )
Place Value 128 64 32 16 8 4 2 1
$a 0 0 0 0 1 0 0 1 = 9
$b 0 0 0 0 1 0 1 0 = 10
$a | $b 0 0 0 0 1 0 1 1 = 11
If you notice we have 3 bits set, in the 8, 2, and 1 column.. add those up 8+2+1 and you get 11.
For mere string concatenation use the dot . operator.
Hope that clarifies it.
It's the OR bitwise operator
If you want to concat string you should use dot
echo "ABC" . "DEF";
Example of OR bitwise usage
// base 16 - result in 0x03
$result = 0x01 | 0x02;
// base 2
0000 0001
0000 0010
---------
0000 0011
That | means 'bitwise OR', which will convert the strings into binary, then overlay them on each other to calculate the result using logical OR for each position i.e. if either string has a 1 at that position, then the result will have a 1, otherwise, you'll get a 0.
In this case, it's doing this with the numerical ascii character codes of each character, which sometimes leads to new character codes and sometimes to garbage, which won't render. This is why the beginning of 'Hello world' is messed up, where it is overlaid with 'test', but the end is fine because it's not having any 1s added to it by another string at that point. See here for a more detailed example from the manual (uses XOR, but same idea).
Use . for concatenation.
I would guess that it's a bitwise OR

How does this php code return odd numbers? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Understanding PHP's & operator
I was just looking at array_filter() function doc and they had the following code to return odd numbers...
<?php
function odd($var)
{
// returns whether the input integer is odd
return($var & 1);
}
?>
Why does $var & 1 return odd number? how does that work?
& is bitwise and. It acts as a mask on the bits of $var. All odd numbers end with 1
no bit &1
1 001 1
2 010 0
3 011 1
4 100 0
5 101 1
6 110 0
7 111 1
You are using a bitwise function with always returns 1 when anded with an odd number.
A few examples:
11 = 3
01 = 1
----
01 = odd -- return 1 (true)
100 = 4
01 = 1
-----
000 = even -- return 0 (false)
One more:
10101 = 21
01 = 1
-------
00001 = odd -- return 1 (true)
That function return 1 if var is an odd number, 0 otherwise. "&" is the AND binary operator, so it considers the last binary digit of a number.
For example:
5 in binary is 101 -> 101 & 1 = 1 -> odd number.
8 in binary is 1000 -> 1000 & 1 = 0 -> even number.

What does the percent sign mean in PHP?

What exactly does this mean?
$number = ( 3 - 2 + 7 ) % 7;
It's the modulus operator, as mentioned, which returns the remainder of a division operation.
Examples: 3%5 returns 3, as 3 divided by 5 is 0 with a remainder of 3.
5 % 10 returns 5, for the same reason, 10 goes into 5 zero times with a remainder of 5.
10 % 5 returns 0, as 10 divided by 5 goes exactly 2 times with no remainder.
In the example you posted, (3 - 2 + 7) works out to 8, giving you 8 % 7, so $number will be 1, which is the remainder of 8/7.
It is the modulus operator:
$a % $b = Remainder of $a
divided by $b.
It is often used to get "one element every N elements". For instance, to only get one element each three elements:
for ($i=0 ; $i<10 ; $i++) {
if ($i % 3 === 0) {
echo $i . '<br />';
}
}
Which gets this output:
0
3
6
9
(Yeah, OK, $i+=3 would have done the trick; but this was just a demo.)
It is the modulus operator. In the statement $a % $b the result is the remainder when $a is divided by $b
Using this operator one can easily calculate odd or even days in month for example, if needed for schedule or something:
<?php echo (date(j) % 2 == 0) ? 'Today is even date' : 'Today is odd date'; ?>
% means modulus.
Modulus is the fancy name for "remainder after divide" in mathematics.
(numerator) mod (denominator) = (remainder)
In PHP
<?php
$n = 13;
$d = 7
$r = "$n % $d";
echo "$r is ($n mod $d).";
?>
In this case, this script will echo
6 is (13 mod 7).
Where $r is for the remainder (answer), $n for the numerator and $d for the denominator. The modulus operator is commonly used in public-key cryptography due to its special characteristic as a one-way function.
Since so many people say "modulus finds the remainder of the divisor", let's start by defining exactly what a remainder is.
In mathematics, the remainder is the amount "left over" after
performing some computation. In arithmetic, the remainder is the
integer "left over" after dividing one integer by another to produce
an integer quotient (integer division).
See: http://en.wikipedia.org/wiki/Remainder
So % (integer modulus) is a simple way of asking, "How much of the divisor is left over after dividing?"
To use the OP's computation of (3 - 2 + 7) = 8 % 7 = 1:
It can be broken down into:
(3 - 2 + 7) = 8
8 / 7 = 1.143 #Rounded up
.143 * 7 = 1.001 #Which results in an integer of 1
7 can go into 8 1 time with .14 of 7 leftover
That's all there is to it. I hope this helps to simplify how exactly modulus works.
Additional examples using different divisors with 21.
Breakdown of 21 % 3 = 0:
21 / 3 = 7.0
3 * 0 = 0
(3 can go into 21 7 times with 0 of 3 leftover)
Breakdown of 21 % 6 = 3:
21 / 6 = 3.5
.5 * 6 = 3
(6 can go into 21 3 times with .5 of 6 leftover)
Breakdown of 21 % 8 = 5:
21 / 8 = 2.625
.625 * 8 = 5
(8 can go into 21 2 times with .625 of 8 leftover)

Categories