note 40994 added to language.operators.bitwise

From: Date: Thu, 25 Mar 2004 20:27:38 +0000
Subject: note 40994 added to language.operators.bitwise
Groups: php.notes 
Request: Send a blank email to php-notes+get-67178@lists.php.net to get a copy of this message
A useful set of functions, making it easier to play with bits as a set of booleans. $bit is a integer between 1 and 32 representing the position of the bit you wish to manipulate. <?php function setbit($val, $bit) { if (readbit($val, $bit)) return $val; return $val += '0x'.dechex(1<<($bit-1)); } function clearbit($val, $bit) { if (!readbit($val, $bit)) return $val; return $val^(0+('0x'.dechex(1<<($bit-1)))); } function readbit($val, $bit) { return ($val&(0+('0x'.dechex(1<<($bit-1)))))?'1':'0'; } function debug($var, $bitlength=32) { for ($j=$bitlength;$j>0;$j--) { echo readbit($var, $j); if ($j%4 == 1) echo ' '; } } ?> ---- Manual Page -- http://www.php.net/manual/en/language.operators.bitwise.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40994 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40994&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40994&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#67178) next »