I understand how bitwise operator & and | work for binary numbers. However I'm confused at how they work with octal numbers.
Does the system convert the number to binary first and then perform the operation and then convert back to octal? Or is it variable?
For example if I do either of these:
echo decoct( 0400 & 0500 | 0000 );
echo decoct( 0600 & 0500 | 0000 );
I get 400 as the result.
However, if i do:
echo decoct( 0400 & 0500 | 0100 );
echo decoct( 0600 & 0500 | 0100 );
I get 500 in both cases.
Also, I have seen code where the bitwise operation in PHP without using decoct around it. Does PHP retain it as octal after completion of the operation? or converts it to decimal (and when does it do that?).
Kindly guide me in the right direction to what to study and learn to understand octal (and decimal and others) bitwise operations.
The bitwise operators "luckily" work for binary, octal and hexadecimal presentations of your values/variables. In the end they all will run on a binary level on your values/variables. The grouping of your digits/bits fits perfectly with the bitwise operators.
0400 = 0b100000000 = 0x100 (0001 0000 0000)
& 0500 = 0b101000000 = 0x140 (0001 0100 0000)
----------------------------
0400 = 0b100000000 = 0x100 (0001 0000 0000)
| 0100 = 0b001000000 = 0x040 (0000 0100 0000)
----------------------------
0500 = 0b101000000 = 0x140 (0001 0100 0000)
And for the other expression:
0600 = 0b110000000 = 0x180 (0001 1000 0000)
& 0500 = 0b101000000 = 0x140 (0001 0100 0000)
----------------------------
0400 = 0b100000000 = 0x100 (0001 0000 0000)
| 0100 = 0b001000000 = 0x040 (0000 0100 0000)
----------------------------
0500 = 0b101000000 = 0x140 (0001 0100 0000)
It does works for these representations because a "digit" will always use 1, 3 or 4 bits and no other "digit" can overwrite these "reserved" digits.
The bitwise operations are run on the bits of the values (hence the name). The representation/display of the values are irrelevant for the bitwise operations.
Related
I'm trying to understand this code in the company's system that I am working in but this doesn't make any sense for me.
$k = 48;
if (($k & 2) > 0) {
echo "2 is true";
}
echo "<br />";
if (($k & 4) > 0) {
echo "4 is true";
}
echo "<br />";
if (($k & 8) > 0) {
echo "8 is true";
}
echo "<br />";
if (($k & 16) > 0) {
echo "16 is true";
}
echo "<br />";
if (($k & 32) > 0) {
echo "32 is true";
}
When $k is 10 or 11, both 2 and 8 is true.
When $k is 12 or 13, both 4 and 8 is true.
Please help me figure this out
This code is simply checking if certain bit positions are set in some value $k. See how bitwise airthmetic works.
In the code you've sent us, it doesn't have much context. However, this is a typical pattern for checking values in a bitmask.
In general, however, the > 0 is unnecessary (since anything !0 typically == true).
It is important to note that when I say bit positions, I mean of the binary number. That is, $k & 32 does not check bit number 32, it instead checks the 6th most significant bit (32d == 0b100000).
Short example
Consider the last statement. We already determined that decimal 32 looks like 100000 in binary. Now say that we want to check whether or not the 6th bit is set in some number 33. 33 is represented as 0b100001 in binary. Now we take the bitwise and which works much like multiplication that we're used to seeing in base 10 (i.e. decimal).
0b100000 <--- decimal 32
&
0b100001 <--- decimal 33
--------
0b100000 <--- Result is > 0 and, therefore, this value has the 6th bit set
Now, let's retry this example with a different number. How about decimal 15.
0b100000 <--- decimal 32
&
0b001111 <--- decimal 15
--------
0b000000 <--- Result == 0 and, therefore, the 6th bit is not set
Now, this & (not to be confused with logical and which is represented as &&) works for multiple bits as well. Say you want to check for bits 6 and 4, then you would want to use 0b101000 (decimal 40), but depending on what you wanted to test (i.e. exactly those bits or just one of those bits), your conditional may change.
Just a little bit more
From a style standpoint it is important to note that these masks are typically represented in hexadecimal instead of decimal (or binary because that would be cumbersome). This is mostly because it is somewhat unclear what binary representation a decimal number correlates to when looking at it "at-a-glance." Let us again consider decimal 32. In hexadecimal, a binary number can be represented as a hexadecimal number by simply grouping bits by 4's (from right to left). See below
0b100000 <--- decimal 32
0000 === 0x0 (hexadecimal 0)
10 === 0b0010 (implied leading 0's) === 0x2 (hexadecimal 2)
--------
0x20
As you can see, hex 20 is decimal 32 is binary 100000.
In your code there are just some bitwise AND operation which checks if some bits are set or not.
So if we take a look what's going on in the first one:
$k = 48;
if (($k & 2) > 0) {
echo "2 is true";
}
The expression ($k & 2) a bit more readable:
0011 0000 $k
0000 0010 2
------------ &
0000 0000 = 0
So if (0 > 0) is false
And another example:
if (($k & 16) > 0) {
echo "16 is true";
}
The expression ($k & 16) a bit more readable:
0011 0000 $k
0001 0000 16
------------ &
0001 0000 = 16
So if (16 > 0) is true
FYI information:
Bitwise AND operation table:
A | B | Result
--------------------
0 | 0 | 0
0 | 1 | 0
1 | 0 | 0
1 | 1 | 1
And as a reference to the manual here: http://php.net/manual/en/language.operators.bitwise.php
for array_filter, I considered an example from php official site. I found a example there
function odd($var)
{
// returns whether the input integer is odd
return($var & 1);
}
function even($var)
{
// returns whether the input integer is even
return(!($var & 1));
}
both function return return($var & 1) and return(!($var & 1));
I can not understand properly what does it mean and specially why 1 is there in return
It's pretty simple: & is a bitwise AND operator and it works as followed:
A | B
---------------
0 | 0 -> 0
1 | 0 -> 0
0 | 1 -> 0
1 | 1 -> 1
And if you take as an example 2, 3 and 6 for odd then this is what's going on:
0000 0010 -> 2
0000 0001 -> 1
--------- &
0000 0000 = return 0
0000 0011 -> 3
0000 0001 -> 1
--------- &
0000 0001 = return 1
0000 0110 -> 6
0000 0001 -> 1
--------- &
0000 0000 = return 0
So in other words if the first digit (1) is 'on' it's going to return 1 because it's odd and if not it's going to return 0
And the same is going on for even just with a NOT (!) operator
<?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
I can't understand how the following code works.
$start = 1;
while($start<10){
if ($start&1) {
echo "ODD ".$start." <br/> ";
}
else {
echo "EVEN ".$start." <br/> ";
}
$start++;
}
The $start&1 will return ODD and EVEN seperately.
Output
ODD 1
EVEN 2
ODD 3
EVEN 4
ODD 5
EVEN 6
ODD 7
EVEN 8
ODD 9
If we give $start&2 instead of $start&1, it returns with another order.
How &1 &2 etc... works here?
It is a bitwise and operator.
0001 --> 1
& 0001
----
0001 --> 1
0010 --> 2
& 0001
----
0000 --> 0
0011 --> 3
& 0001
----
0001 --> 1
Depending on the endianness, it will be either the leftmost or rightmost bit that matters in this check. The above is &ing with 1. In your second example, &ing with 2, that would be
0001 --> 1
& 0010
----
0000 --> 0
0010 --> 2
& 0010
----
0010 --> 2
0011 --> 3
& 0010
----
0010 --> 2
And for further comparison, here is 1-3 &ing with 3
0001 --> 1
& 0011
----
0001 --> 1
0010 --> 2
& 0011
----
0010 --> 2
0011 --> 3
& 0011
----
0011 --> 3
To see what is going on, follow the columns of the two numbers down. If they are both a 1 then the result has the bit set in that position to a 1. If either are a 0 then the result is a 0 in that position. So for 2 & 3..
0010 --> 2
& 0011
----
0010
||||
|||+- 0 and 1, so 0
||+-- 1 and 1, so 1
++--- 0 and 0, so 0
0010 == 2
This code is based on the & operator (the AND bitwise operator):
$start&1 will return true if the rightmost bit is 1 => the number is odd
For example, the binary representation of 5 (odd) is:
101
and 1:
001
So 101 & 001 will return 001 which is also true in PHP.
The bitwise operator & deals with bits. On computers numbers are stored as binary data (1's and 0's, also called "bits"). Here is a table of the numbers that are used in the loop.
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
Unlike decimal numbers, binary numbers are read right to left. When you look at the table you can see that even and odd binary numbers end in 0 and 1, respectively. Binary numbers that end in 0 are even, and those that end in 1 are odd.
What the & bitwise operator does is basically chip off the last digit from the binary representation of the decimal, and then returns it.
If we take the number 5 (0101) then we can set it up in pseudocode:
if last bit in 0101 is 1 then
number is odd
otherwise
number is even
As for the PHP code the expression ($start & 1) returns either 1 or 0. In PHP type juggling turns 1 into true, and 0 into false.
It loops 9 times, each time it bitwise ands with 1 (that is it ands with the second binary digit to see if it is a mutliple of 2) and prints accordingly
& is the bitwise and operator. In the expression, it's looking for births the two values share in common.
Here's a tutorial of PHP bitwise operator usage: http://www.w3resource.com/php/operators/bitwise-operators.php
I have 5 bits and so 32 different combinations (of them).
Starting from
00000
and ending with
11111
Is there some way of quickly listing all possibilities? I could do it by hand, but I worry that I might miss one. I'm guessing some clever chap has written some algorithm and/or made a website, that can do this very easily. At least I hope so.
Thanks a lot.
This will put them all on the command line on Linux.
echo {0..1}{0..1}{0..1}{0..1}{0..1}
In Ruby:
0b0000.upto(0b1111) {|n| puts n.to_s(2).rjust(4,"0")}
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Write a column with integer from 0 to 31, then write a second column with the binary equivalent of each integer side-by-side.
That way you will increase your chance not to miss a combination.
Just count from 0 to 31 and output the digit in it's binary form.
Something like this should do:
public static String zeroPad(String str) {
return "00000".substring(str.length()) + str;
}
public static void main(String[] args) {
for (int i = 0; i < 32; i++)
System.out.printf("%s%n", zeroPad(Integer.toBinaryString(i)));
}
Output:
00000
00001
00010
00011
...
11110
11111
for (int i = 0; i <31; i++)
cout << ((i & 16) >> 4) << ((i & 8) >> 3) << ((i & 4) >> 2)
<< ((i & 2) >> 1) << (i & 1) << endl;
Unix:
echo {0..1}{0..1}{0..1}{0..1} | xargs -n 1