Monday, November 21, 2011

PHP Codes to Force a Number to Be a Negative Number

$number = ~abs($number) + 1;

~ is to invert all the bits in PHP and + 1 is to make it a negative number. Remember in CS101 that binary number can be converted to represent a negative number by invert all the bits and +1?

abs() is to make sure it is not a negative number and force it to be a positive number even though it is a negative number. Inverting a negative number will only make it worse.

No comments:

Post a Comment