note 57036 deleted from language.operators.bitwise by danielc

From: Date: Mon, 13 Apr 2009 16:14:37 +0000
Subject: note 57036 deleted from language.operators.bitwise by danielc
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153173@lists.php.net to get a copy of this message
Note Submitter: junk at spheresys dot com Reason: 2 ---- I wanted to print a binary representation of an integer here it is <?php function binString($var, $safety = 0) { return ($var & 1) . ($var != 0 && $safety < 31 ? binString($var >> 1, $safety+1) : ""); } print binString(5); // would print 101 print binString(15); // would print 1111 ?> Explanation: the first expression after the return statement will evaluate to 1 or 0 according to the least significant bit. then we have our stop condition if there is still non zero bits left or if we are at the 32nd bit otherwise we call recursively on to the n-1 bits (by shifting out the least significant bit).

« previous php.notes (#153173) next »