Jump to content

strange Bitwise results


Dizzee

Recommended Posts

Hi I've been playing with the bitwise operator and some binary and have hit a wall

 

the code should work ::), but it stops working when the number gets to 10 digits long or more

I've created the following to demonstrate

could someone please explain why it is doing this and possably how to fix it

thanks

 

 

<?php
print_r($_POST);
if (!$_POST['role']) {
$myroles = "72057594037927808";
$my_lengh = 9;
} else {
$myroles = $_POST['role'];
$my_lengh = $_POST['mylengh'];
}
?>
<form action="" method="post">
    <p>Number: 
      <input type="text" name="role" value="<?php echo $myroles; ?>" />
      <br />truncate to:
      <input name="mylengh" type="text" value="<?php echo $my_lengh; ?>" size="2" maxlength="2"/>
      <br />
      <input type="submit" name="submit" value="Submit me!" />
  </p>
</form>
<?php
echo "<hr>";

echo "role number = $myroles<p>";
$roles = substr($myroles, 0-$my_lengh, $my_lengh);
echo "Used number = $roles<p>";

if ($roles & 1) {

echo "bitwise 2<sup>0</sup><BR>";
}

if ($roles & (128)) {
echo "bitwise 2<sup>7</sup><BR>";
}

if ($roles & (256)) {
echo "bitwise 2<sup>8</sup><BR>";
}

if ($roles & (512)) {
echo "bitwise 2<sup>9</sup><BR>";
}

if ($roles & (1024)) {
echo "bitwise 2<sup>10</sup><BR>";
}

if ($roles & (2048)) {
echo "bitwise 2<sup>11</sup><BR>";
}

if ($roles & (4096)) {
echo "bitwise 2<sup>12</sup><BR>";
}

if ($roles & (8192)) {
echo "bitwise 2<sup>13</sup><BR>";
}

if ($roles & (16384)) {
echo "bitwise 2<sup>14</sup><BR>";
}

if ($roles & (32768)) {
echo "bitwise 2<sup>15</sup><BR>";
}

if ($roles & (65536)) {
echo "bitwise 2<sup>16</sup><BR>";
}

if ($roles & (131072)) {
echo "bitwise 2<sup>17</sup><BR>";
}

if ($roles & (262144)) {
echo "bitwise 2<sup>18</sup><BR>";
}

if ($roles & (524288)) {
echo "bitwise 2<sup>19</sup><BR>";
}

if ($roles & (1048576)) {
echo "bitwise 2<sup>20</sup><BR>";
}

if ($roles & (2097152)) {
echo "bitwise 2<sup>21</sup><BR>";
}

if ($roles & (4194304)) {
echo "bitwise 2<sup>22</sup><BR>";
}

if ($roles & (8388608)) {
echo "bitwise 2<sup>23</sup><BR>";
}

if ($roles & (16777216)) {
echo "bitwise 2<sup>24</sup><BR>";
}

if ($roles & (33554432)) {
echo "bitwise 2<sup>25</sup><BR>";
}

if ($roles & (67108864)) {
echo "bitwise 2<sup>26</sup><BR>";
}

if ($roles & (134217728)) {
echo "bitwise 2<sup>27</sup><BR>";
}

if ($roles & (268435456)) {
echo "bitwise 2<sup>28</sup><BR>";
}

if ($roles & (536870912)) {
echo "bitwise 2<sup>29</sup><BR>";
}

if ($roles & (1073741824)) {
echo "bitwise 2<sup>30</sup><BR>";
}

if ($roles & (2147483648)) {
echo "bitwise 2<sup>31</sup><BR>";
}

if ($roles & (4294967296)) {
echo "bitwise 2<sup>32</sup><BR>";
}

if ($roles & (8589934592)) {
echo "bitwise 2<sup>33</sup><BR>";
}

if ($roles & (17179869184)) {
echo "bitwise 2<sup>34</sup><BR>";
}

if ($roles & (34359738368)) {
echo "bitwise 2<sup>35</sup><BR>";
}

if ($roles & (68719476736)) {
echo "bitwise 2<sup>36</sup><BR>";
}

if ($roles & (137438953472)) {
echo "bitwise 2<sup>37</sup><BR>";
}

if ($roles & (274877906944)) {
echo "bitwise 2<sup>38</sup><BR>";
}

if ($roles & (549755813888)) {
echo "bitwise 2<sup>39</sup><BR>";
}

if ($roles & (1099511627776)) {
echo "bitwise 2<sup>40</sup><BR>";
}

if ($roles & (2199023255552)) {
echo "bitwise 2<sup>41</sup><BR>";
}
?>

Link to comment
Share on other sites

FYI, here is the output on a 64 bit php:

 

2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
2^9 = 512
2^10 = 1024
2^11 = 2048
2^12 = 4096
2^13 = 8192
2^14 = 16384
2^15 = 32768
2^16 = 65536
2^17 = 131072
2^18 = 262144
2^19 = 524288
2^20 = 1048576
2^21 = 2097152
2^22 = 4194304
2^23 = 8388608
2^24 = 16777216
2^25 = 33554432
2^26 = 67108864
2^27 = 134217728
2^28 = 268435456
2^29 = 536870912
2^30 = 1073741824
2^31 = 2147483648
2^32 = 4294967296
2^33 = 8589934592
2^34 = 17179869184
2^35 = 34359738368
2^36 = 68719476736
2^37 = 137438953472
2^38 = 274877906944
2^39 = 549755813888
2^40 = 1099511627776

 

That'll give you at least 63 bits to play with (and the 64th too if you are careful with it).  But again there is a hard limit based on the size of an integer.

Link to comment
Share on other sites

Yup, thanks I see the problem now, If I remove intval I get the correct return?

I've been searching but can't find a work around, the best I found was for Xor

function unsigned_xor32 ($a, $b)
{
        $a1 = $a & 0x7FFF0000;
        $a2 = $a & 0x0000FFFF;
        $a3 = $a & 0x80000000;
        $b1 = $b & 0x7FFF0000;
        $b2 = $b & 0x0000FFFF;
        $b3 = $b & 0x80000000;

        $c = ($a3 != $b3) ? 0x80000000 : 0;

        return (($a1 ^ $b1) |($a2 ^ $b2)) + $c;
}

I'm looking at dealing with it, but any points in the right direction would be appreciated,

I'm thinking either converting from a float or converting to hex??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.