note 75495 deleted from language.operators.bitwise by danielc

From: Date: Mon, 13 Apr 2009 16:15:30 +0000
Subject: note 75495 deleted from language.operators.bitwise by danielc
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153178@lists.php.net to get a copy of this message
Note Submitter: kendsnyder at gmail dot com Reason: 2 ---- I needed to perform a bitwise AND on floats and came up with this. There is probably a better way to do this with bit rotation, but this works. /** * Perform a float-safe bitwise AND comparison * * @param int/float $number The number to which to perform a bitwise AND * @param int/float $comparison The number with which to perform a bitwise AND * @return bool */ function safeBitCheck($number,$comparison) { if( $number < 2147483647 ) { return ($number & $comparison)==$comparison; } else { $binNumber = strrev(base_convert($number,10,2)); $binComparison = strrev(base_convert($comparison,10,2)); for( $i=0; $i<strlen($binComparison); $i++ ) { if( strlen($binNumber)<$i || ($binComparison{$i}==="1" && $binNumber{$i}==="0") ) { return false; } } return true; } } safeBitCheck(17,16); // true safeBitCheck(17,2); // false safeBitCheck((4294967296+8589934592),4294967296); // true safeBitCheck(2,8589934592); // false

« previous php.notes (#153178) next »