note 84305 added to language.operators.bitwise

From: Date: Tue, 08 Jul 2008 18:23:13 +0000
Subject: note 84305 added to language.operators.bitwise
Groups: php.notes 
Request: Send a blank email to php-notes+get-141275@lists.php.net to get a copy of this message
Quick reminder about an easy gotcha: When using the ~ operator on variables make sure you know what the variable's type is -- typecast when unsure. I learned this the hard way when using the ~ and & operators to test bits in an integer context, without realizing that one of my variables was actually initialized as a string: <?php $foo = 0; // $foo is int $bar = '0'; // $bar is string . . . // This works if (~$foo & 0x20) { ... } // This doesn't work if (~$bar & 0x20) { ... } ?> The first case is straightforward; the second case is a little more underhanded: - ($bar) is string, so the ~ operator will use its ASCII value, 0x30 - (~$bar) returns ASCII character 0xFFCF (on 32-bit systems) - (0x20) is an integer, so the & operator will use integer values - (~$bar) is not a number, so its integer value is zero - (~$bar & 0x20) equals zero In my case the variable was a class member initialized from an include()'d file, so it took a good while to hunt down.... ---- Server IP: 69.147.83.197 Probable Submitter: 74.4.179.5 ---- Manual Page -- http://www.php.net/manual/en/language.operators.bitwise.php Edit -- https://master.php.net/note/edit/84305 Del: integrated -- https://master.php.net/note/delete/84305/integrated Del: useless -- https://master.php.net/note/delete/84305/useless Del: bad code -- https://master.php.net/note/delete/84305/bad+code Del: spam -- https://master.php.net/note/delete/84305/spam Del: non-english -- https://master.php.net/note/delete/84305/non-english Del: in docs -- https://master.php.net/note/delete/84305/in+docs Del: other reasons-- https://master.php.net/note/delete/84305 Reject -- https://master.php.net/note/reject/84305 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141275) next »