Afficher un message
Vieux 08/04/2008, 14h50   #2
Mark J. Reed
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Could someone tell me what a tilde(~) in PHP does ?

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


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@mail.com>
  Réponse avec citation
 
Page generated in 0,05444 seconds with 9 queries