Re: Fun with Binary Numbers
| From: | Rasmus Lerdorf | Date: | Wed, 28 Aug 2002 14:45:05 +0000 |
| Subject: | Re: Fun with Binary Numbers | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-114373@lists.php.net to get a copy of this message | ||
Well, to begin with applying the mask 1111 0000 to 1101 0100 0110 0110 is
going to give you 1100000 not 110. But you can shift the result to the
right 4 if that is the result you want. There is no algorithm to speak
of here, just apply the mask. Like this:
$num = "1101010001100110";
$mask = "11110000";
$result = bindec($num) & bindec($mask);
echo decbin($result);
If you want only the second quartet of the result, do:
$result = $result>>4;
-Rasmus
On Wed, 28 Aug 2002, David Buerer wrote:
> Here's a question,
>
> Say I have a binary number like this
>
> 1101 0100 0110 0110
>
> And I want to pull out of the binary number the third quartet (is that the
> right word)
>
> I want to know how to apply the mask
>
> 0000 1111 0000 0000
>
> to the number above so that the result would be
> 0100
>
> Likewise, given
> the number: 1101 0100 0110 0110
> and the mask 0000 0000 1111 0000
> the result 0110
>
> I suppose this is the same algorithm used to separate the host from the node
> address given an IP address and a subnet mask. I just need to know what the
> algorithm is.
>
> Thanks
> David
>
>