note 58186 added to language.operators.bitwise

From: Date: Thu, 27 Oct 2005 00:22:06 +0000
Subject: note 58186 added to language.operators.bitwise
Groups: php.notes 
Request: Send a blank email to php-notes+get-97645@lists.php.net to get a copy of this message
Here're my 32-bit carry-discarding operations for those of you porting encryption algorithms from C. Be warned that some of these are not very efficient compared to the native operations, especially when called by heavy-duty encryption algorithms - but not discarding the carry bit may not land you the same results you get in C, simply because PHP's bitwise operations were not designed to work on fixed-sized registers. (If your ported encryption algo still doen't give you the same results, remember to check your Endian-ness!) function _BF_SHR32 ($x, $bits) { if ($bits==0) return $x; if ($bits==32) return 0; $y = ($x & 0x7FFFFFFF) >> $bits; if (0x80000000 & $x) { $y |= (1<<(31-$bits)); } return $y; } function _BF_SHL32 ($x, $bits) { if ($bits==0) return $x; if ($bits==32) return 0; $mask = (1<<(32-$bits)) - 1; return (($x & $mask) << $bits) & 0xFFFFFFFF; } function _BF_GETBYTE ($x, $y) { return _BF_SHR32 ($x, 8 * $y) & 0xFF; } function _BF_OR32 ($x, $y) { return ($x | $y) & 0xFFFFFFFF; } function _BF_ADD32 ($x, $y) { $x = $x & 0xFFFFFFFF; $y = $y & 0xFFFFFFFF; $total = 0; $carry = 0; for ($i=0; $i<4; $i++) { $byte_x = _BF_GETBYTE($x, $i); $byte_y = _BF_GETBYTE($y, $i); $sum = $byte_x + $byte_y; $result = $sum & 0xFF; $carryforward = _BF_SHR32($sum, 8); $sum = $result + $carry; $result = $sum & 0xFF; $carry = $carryforward + _BF_SHR32($sum, 8); $total = _BF_OR32(_BF_SHL32($result, $i*8), $total); } return $total; } ---- Manual Page -- http://www.php.net/manual/en/language.operators.bitwise.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58186 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58186&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58186&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58186&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58186&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58186&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58186&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58186&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58186&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97645) next »