Re: Could someone tell me what a tilde(~) in PHP does ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Apr 8, 2008 at 6:19 AM, Tony Collings <tony@xxxxxxxxxxxxxxxx> wrote:
> The humble tilde (~). I came across it the other day in some PHP code
>  [code]~E_ERROR[/code]

That comes from C, not postpositional math.  It's  bitwise negation.
That is, the number 47 in binary is 110001, with leading 0's out to
whatever the word size is, usually 32
or 64 bits.  The ~ flips the bits, so on a 32-bit system ~47 is binary
11111111111111111111111111001110, which is -48 if you treat it as a
signed value and
429496248 as unsigned.

The tilde is most often seen in the company of flag values, where each bit
represents an option that is on or off.  Typically, you have a bunch
of constants
defined as the individual bit values, like say E_DEBUG.  Then ~E_DEBUG
means "turn on everything except E_DEBUG".   Often used  with bitwise
AND (&) as a mask to turn a particular bit off, as in <?php $flags &=
~E_DEBUG ?> which turns off
E_DEBUG while leaving the other bits in $flag unchanged.

-- 
Mark J. Reed <markjreed@xxxxxxxx>

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux