Hey Dave,

It is much easier to use hex numbers when working with bits. Thus, the mask
for the first (left) "quartet" would be 0xF000,
second 0x0F00, third 0x00F0 and fourth 0x000F. To apply the mask use binary
AND operator, which is "&" in case of PHP.
After applying the mask you need to "shift-right" (another binary operator)
your number to the certain number of bits you want to omit:
$result = ($yourNumber & 0x0F00) >> 8; // for the third "quartet"

Hope this helps,
Stas

----- Original Message -----
From: "David Buerer" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, August 28, 2002 3:11 PM
Subject: [PHP] Fun with Binary Numbers


> 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
>
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to