@GSMC/서용덕: DMD Programming
[Steve Oualline] 11. Bit Operations
maetel
2008. 5. 14. 12:03
Steve Oualline
11. Bit Operations
A bit is the smallest unit of information. Normally, it is represented by the values 1 and 0. Bit manipulations are used to control the machine at the lowest level. (156p)
compound operators (&=, |=)
http://www.cplusplus.com/doc/tutorial/operators.html
ref.
http://en.wikipedia.org/wiki/Parity_bit
http://en.wikipedia.org/wiki/Discrete_mathematics
166p
Example 11-2
실행 결과:
char인 flags와 int인 DIRECT_CONNECT를 비교하면 8bits와 32bits를 비교하는 셈이다. 이때 8bits를 늘려 32bits에 맞추어 고려하게 된다. (XCode에서 컴파일하면, 추가된 자리에 통상 (signed일 때) 1을 집어 넣고 unsigned의 경우 0을 집어 넣는다. 그래서 flags를 그냥(signed) char로 선언한 경우 다음과 같이 되어 flags & DIRECT_CONNECT 가 0이 아니게 되었던 것이다.)
한편, flags를 int로 선언하고 실행하면 결과가 다음과 같다. (signed일 때와 unsigned일 때 모두 동일한 결과)
In bitmapped graphics, each pixel on the screen is represented by a single bit in memory. (167p)
11. Bit Operations
A bit is the smallest unit of information. Normally, it is represented by the values 1 and 0. Bit manipulations are used to control the machine at the lowest level. (156p)
compound operators (&=, |=)
http://www.cplusplus.com/doc/tutorial/operators.html
ref.
http://en.wikipedia.org/wiki/Parity_bit
http://en.wikipedia.org/wiki/Discrete_mathematics
166p
Example 11-2
#include <stdio.h>
const unsigned int HIGH_SPEED = (1<<7);
const unsigned int DIRECT_CONNECT = (1<<8);
unsigned char flags = 0;
int main()
{
flags |= HIGH_SPEED;
flags |= DIRECT_CONNECT;
if ((flags & HIGH_SPEED) != 0)
printf("High speed set: flags = %d %x\n", flags, flags);
if ((flags & DIRECT_CONNECT) != 0)
printf("Direct connect set: flags = %d %x\n", flags, flags);
return(0);
}
const unsigned int HIGH_SPEED = (1<<7);
const unsigned int DIRECT_CONNECT = (1<<8);
unsigned char flags = 0;
int main()
{
flags |= HIGH_SPEED;
flags |= DIRECT_CONNECT;
if ((flags & HIGH_SPEED) != 0)
printf("High speed set: flags = %d %x\n", flags, flags);
if ((flags & DIRECT_CONNECT) != 0)
printf("Direct connect set: flags = %d %x\n", flags, flags);
return(0);
}
실행 결과:
High speed set: flags = 128 80
char인 flags와 int인 DIRECT_CONNECT를 비교하면 8bits와 32bits를 비교하는 셈이다. 이때 8bits를 늘려 32bits에 맞추어 고려하게 된다. (XCode에서 컴파일하면, 추가된 자리에 통상 (signed일 때) 1을 집어 넣고 unsigned의 경우 0을 집어 넣는다. 그래서 flags를 그냥(signed) char로 선언한 경우 다음과 같이 되어 flags & DIRECT_CONNECT 가 0이 아니게 되었던 것이다.)
High speed set: flags = -128 ffffff80
Direct connect set: flags = -128 ffffff80
Direct connect set: flags = -128 ffffff80
한편, flags를 int로 선언하고 실행하면 결과가 다음과 같다. (signed일 때와 unsigned일 때 모두 동일한 결과)
High speed set: flags = 384 180
Direct connect set: flags = 384 180
Direct connect set: flags = 384 180
In bitmapped graphics, each pixel on the screen is represented by a single bit in memory. (167p)