Q&A

  • 비트연산
델파이에서 비트 연산이 가능한지 알고 싶고
혹시 c에서
if (ss & 1)
이게 무슨 뜻인지 아십니까?
1  COMMENTS
  • Profile
    장지용 2002.03.22 10:15
    참고하세요.

    The following logical operators perform bitwise manipulation on integer operands. For example, if the value stored in X (in binary) is 001101 and  the value stored in Y is 100001, the statement

    Z := X or Y;

    assigns the value 101101 to Z.

    Operator       Operation         Operand types    Result type     Examples
    not         bitwise negation        integer             integer            not X
    and        bitwise and               integer             integer            X and Y
    or          bitwise or                  integer             integer            X or Y
    xor         bitwise xor                integer             integer            X xor Y
    shl         bitwise shift left          integer             integer            X shl 2
    shr         bitwise shift right        integer             integer            Y shl I

    The following rules apply to bitwise operators.

    The result of a not operation is of the same type as the operand.
    If the operands of an and, or, or xor operation are both integers, the result is of the predefined integer type with the smallest range that includes all possible values of both types.
            
    The operations x shl y and x shr y shift the value of x to the left or right by y bits, which is equivalent to multiplying or dividing x by 2^y;
    the result is of the same type as x. For example, if N stores the value 01101 (decimal 13), then N shl 1 returns 11010 (decimal 26).

    ---------------------------------------------------------------
    Bitwise 연산자
    * & : 비트단위 논리곱
    * | : 비트단위 논리합
    * ^  : 비트단위 배타적 논리합


    Bitwise연산자    논리곱     논리합     배타적 논리합
    x      y             x & y       x | y            x ^ y
    0      0                0             0                  0
    0      1                0             1                  1
    1      0                0             1                  1
    1      1                1             1                  0


    예제)

    #include <stdio.h>

    void main()
    {
      int a=10, b=22, c, d, e;

      c = a & b;  //c=2
      d = a | b;  //d=30
      e = a ^ b;  //e=28

      printf("c=%d, d=%d, e=%d", c, d, e);
    }

    a=0000000000001010
    b=0000000000010110
    c=0000000000000010
    d=0000000000011110
    e=0000000000011100
    ---------------------------------------------------------------