블로그 이미지
Leeway is... the freedom that someone has to take the action they want to or to change their plans.
maetel

Notice

Recent Post

Recent Comment

Recent Trackback

Archive

calendar

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 31
  • total
  • today
  • yesterday

Category

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

실행 결과:
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

한편, flags를 int로 선언하고 실행하면 결과가 다음과 같다. (signed일 때와 unsigned일 때 모두 동일한 결과)

High speed 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)



'@GSMC > 서용덕: DMD Programming' 카테고리의 다른 글

[Steve Oualline] 12. Advanced Types  (0) 2008.05.28
data type 정리  (0) 2008.05.24
append()  (0) 2008.05.01
[Steve Oualline] 10. C Preprocessor  (0) 2008.04.22
hw 6 - 9장 연습문제 3번  (0) 2008.04.17
posted by maetel