PHP에서 16 Bit 연산과 32 Bit 연산의 결과가 차이가 있고, Shift 연산의 경우에도 부호 비트까지 포함하여 Shift 연산을 수행하느냐, 그렇지 않느냐에 따라 결과가 확연하게 달라짐.
앞서 올린 암호화/복호화 PHP 프로그램에서도 리눅스 시스템 환경에 따라 동일한 결과가 아닌 다른 결과가 나올 수 있으므로, SHIFT 연산의 경우는 아래의 32 비트 연산 함수를 이용하여 처리를 해야 하며, Integer Overflow 연산의 경우에도 (※ PHP 특정 버전에서만 그런것인지 리눅스 시스템 환경에 영향을 받는것인지 확실하지 않음) 두개의 리눅스 시스템에서 다른 결과가 나왔음.
양수 값의 오버플로우 연산의 경우에는 정상적인 연산 오버플로우 처리가 되어, 원하는 결과를 얻을 수 있었으나 음수 값을 가진 오버플로우 연산의 경우에는 시스템의 특성을 타는 것을 확인하였음. 이에 음수 값을 가진 연산의 경우에는 양수로의 전환후 연산한 후 (연산전 -1을 곱하고, 연산 완료후 -1을 곱해준다.) 처리하여 원하는 결과를 얻을 수 있었음.
비트 연산에 대한 내용을 위키피디아[http://en.wikipedia.org])에서 발췌하였음.
Logical shift
![]() Logical shift left | ![]() Logical shift right |
Rotate no carry
![]() Left circular shift or rotate | ![]() Right circular shift or rotate |
Rotate through carry
Rotate through carry is similar to the rotate no carry operation, but the two ends of the register are considered to be separated by the carry flag. The bit that is shifted in (on either end) is the old value of the carry flag, and the bit that is shifted out (on the other end) becomes the new value of the carry flag.
A single rotate through carry can simulate a logical or arithmetic shift of one position by setting up the carry flag beforehand. For example, if the carry flag contains 0, then x RIGHT-ROTATE-THROUGH-CARRY-BY-ONE is a logical right-shift, and if the carry flag contains a copy of the sign bit, then x RIGHT-ROTATE-THROUGH-CARRY-BY-ONE is an arithmetic right-shift. For this reason, some microcontrollers such as PICs just have rotate and rotate through carry, and don't bother with arithmetic or logical shift instructions.
Rotate through carry is especially useful when performing shifts on numbers larger than the processor's native word size, because if a large number is stored in two registers, the bit that is shifted off the end of the first register must come in at the other end of the second. With rotate-through-carry, that bit is "saved" in the carry flag during the first shift, ready to shift in during the second shift without any extra preparation.
![]() Left rotate through carry | ![]() Right rotate through carry |
PHP 비트 연산 함수 응용 예
FUNCTION ASCII2HEX($ASCII) { $len = strlen($ASCII); for ($i=0; $i<$len; $i++) $result.=sprintf("%02X",ord(substr($ASCII,$i,1))); return $result; } FUNCTION HEX2ASCII($HEX) { $len = strlen($HEX); $result = ''; for ($i=0; $i<$len; $i+=2) { $result.=sprintf("%s", chr(hexdec(substr($HEX,$i,2)))); } return $result; } FUNCTION _BF_SHR_32($value, $count) { if ($count==0) return $value; if ($count==32) return 0; $result = ($value & 0x7FFFFFFF) >> $count; if (0x80000000 & $value) { $result |= (1<<(31-$count)); } return $result; } FUNCTION _BF_SHL_32($value, $count) { if ($count==0) return $value; if ($count==32) return 0; $mask = (1 << (32 - $count)) - 1; return (($value & $mask) << $count) & 0xFFFFFFFF; } FUNCTION _BF_GETBYTE($value, $pos) { return SHIFT_RIGHT_32($value, 8 * $pos) & 0xFF; } FUNCTION _BF_OR_32($value1, $value2) { return ($value1 | $value2) & 0xFFFFFFFF; } FUNCTION _BF_ADD_32($value1, $value2) { $value1 = $value1 & 0xFFFFFFFF; $value2 = $value2 & 0xFFFFFFFF; $total = 0; $carry = 0; for ($i=0; $i<4; $i++) { $byte_x = _BF_GETBYTE($value1, $i); $byte_y = _BF_GETBYTE($value2, $i); $sum = $byte_x + $byte_y; $result = $sum & 0xFF; $carryforward = _BF_SHR_32($sum, 8); $sum = $result + $carry; $result = $sum & 0xFF; $carry = $carryforward + _BF_SHR_32($sum, 8); $total = _BF_OR32(_BF_SHL_32($result, $i*8), $total); } return $total; }