마음의 안정을 찾기 위하여 - 비트 연산에 대한 이해와 PHP에서의 응용 예
2275951
474
489
관리자새글쓰기
태그위치로그방명록
별일없다의 생각
dawnsea's me2day/2010
색상(RGB)코드 추출기(Color...
Connection Generator/2010
최승호PD, '4대강 거짓말 검...
Green Monkey**/2010
Syng의 생각
syng's me2DAY/2010
천재 작곡가 윤일상이 기획,...
엘븐킹's Digital Factory/2010
비트 연산에 대한 이해와 PHP에서의 응용 예
PHP | 2008/02/28 10:05

PHP에서 16 Bit 연산과 32 Bit 연산의 결과가 차이가 있고, Shift 연산의 경우에도 부호 비트까지 포함하여 Shift 연산을 수행하느냐, 그렇지 않느냐에 따라 결과가 확연하게 달라짐.

앞서 올린 암호화/복호화 PHP 프로그램에서도 리눅스 시스템 환경에 따라 동일한 결과가 아닌 다른 결과가 나올 수 있으므로, SHIFT 연산의 경우는 아래의 32 비트 연산 함수를 이용하여 처리를 해야 하며, Integer Overflow 연산의 경우에도 (※ PHP 특정 버전에서만 그런것인지 리눅스 시스템 환경에 영향을 받는것인지 확실하지 않음) 두개의 리눅스 시스템에서 다른 결과가 나왔음.

양수 값의 오버플로우 연산의 경우에는 정상적인 연산 오버플로우 처리가 되어, 원하는 결과를 얻을 수 있었으나 음수 값을 가진 오버플로우 연산의 경우에는 시스템의 특성을 타는 것을 확인하였음. 이에 음수 값을 가진 연산의 경우에는 양수로의 전환후 연산한 후 (연산전 -1을 곱하고, 연산 완료후 -1을 곱해준다.) 처리하여 원하는 결과를 얻을 수 있었음.

비트 연산에 대한 내용을 위키피디아[http://en.wikipedia.org])에서 발췌하였음.

Logical shift

In a logical shift, the bits that are shifted out are discarded, and zeros are shifted in (on either end). Therefore, the logical and arithmetic left-shifts are exactly the same operation. However, the logical right-shift inserts bits with value 0 instead of copies of the sign bit. Hence the logical shift is suitable for unsigned binary numbers, while the arithmetic shift is suitable for signed two's complement binary numbers.

사용자 삽입 이미지

Logical shift left

사용자 삽입 이미지

Logical shift right


Rotate no carry

Another form of shift is the circular shift or bit rotation. In this operation, the bits are "rotated" as if the left and right ends of the register were joined. The value that is shifted in on the right during a left-shift is whatever value was shifted out on the left, and vice versa. This operation is useful if it is necessary to retain all the existing bits, and is frequently used in digital cryptography.

사용자 삽입 이미지

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;
}

2008/02/28 10:05 2008/02/28 10:05
Article tag list Go to top
View Comment 0
Trackback URL :: 이 글에는 트랙백을 보낼 수 없습니다
 
 
 
 
: [1] ... [622][623][624][625][626][627][628][629][630] ... [1317] :
«   2024/04   »
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
전체 (1317)
출판 준비 (0)
My-Pro... (41)
사는 ... (933)
블로그... (22)
My Lib... (32)
게임 ... (23)
개발관... (3)
Smart ... (1)
Delphi (93)
C Builder (0)
Object... (0)
VC, MF... (10)
Window... (1)
Open API (3)
Visual... (0)
Java, JSP (2)
ASP.NET (0)
PHP (5)
Database (12)
리눅스 (29)
Windows (25)
Device... (1)
Embedded (1)
게임 ... (0)
Web Se... (2)
Web, S... (21)
잡다한... (6)
프로젝트 (0)
Personal (0)
대통령... (13)
Link (2)