연산자 - seoul national universityropas.snu.ac.kr/~gslee/lecture-slides/c2011-01/20110330.pdf ·...

32
비트 연산자 1 1 Thursday, March 31,

Upload: others

Post on 25-Apr-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

비트 연산자

1

1Thursday, March 31,

비트와 바이트

2

►비트와 바이트 비트(Bit) : 2진수 값 하나(0 또는 1)를 저장할 수 있는 최소 메모리 공간

1바이트는 8비트

1비트 2비트 3비트 ... n비트2^1 = 2개 2^2 = 4개 2^3 = 8개 ... 2^n 개

5.3 (1/19)!

! (Bit) : 2 (0 1)

!

! 1 8

1 2 3 … n2!=2 2!=4 2!=8

!

… 2!

2Thursday, March 31,

진수법

3

5.3 (2/19)!2 , 10 , 16 , 8

! 2 : 0~1 ( )

! 10 : 0~9 ( )

! 16 : 0~9 , 9 a, b, c, d, e, f ( )

! 8 : 0~7 ( )

3Thursday, March 31,

진수법

4

2 10 16 8

0000 0000 0 0 00000 0001 1 1 10000 0010 2 2 20000 0011 3 3 30000 0100 4 4 40000 0101 5 5 50000 0110 6 6 60000 0111 7 7 70000 1000 8 8 10

2 10 16 8

0000 1001 9 9 110000 1010 10 a 12

0000 1011 11 b 130000 1100 12 c 140000 1101 13 d 150000 1110 14 e 160000 1111 15 f 170001 0000 16 10 200001 0001 17 11 21

(2 , 10 , 16 , 8 )

10 == 0xa == 012

4Thursday, March 31,

2진수를 10진수로 표현하는 방법

5

5.3 (3/19)!2 10

2 10 2 10 2 100000 0000 0000 0110 0000 11000000 0001 0000 0111 0000 11010000 0010 0000 1000 0000 11100000 0011 0000 1001 0000 11110000 0100 0000 1010 0001 0000 0000 0101 0000 1011 0001 0001

5Thursday, March 31,

2진수를 16진수와 8진수로 표현하는 방법

6

5.3 (4/19)!2 16 8

6Thursday, March 31,

비트 연산자

7

5.3 (5/19) !

& a & b AND

| a | b OR

^ a ^ b XOR

~ ~a NOT

<< a << 3

>> a >> 1

7Thursday, March 31,

비트 연산자: & 연산자(1/2)

8

5.3 (6/19)!&

! AND ! 1 1

0 & 0 00 & 1 01 & 0 01 & 1 1

8Thursday, March 31,

비트 연산자: & 연산자(2/2)

9

5.3 (7/19)---[5-13.c ]

#include<stdio.h>int main(void){ int num1=20, num2=16; int result1;

result1 = num1 & num2; printf(" & %d \n", result1); // 16 return 0;}

9Thursday, March 31,

비트 연산자: | 연산자(1/2)

10

5.3 (8/19)! |

! OR ! 1 1

0 | 0 00 | 1 11 | 0 11 | 1 1

10Thursday, March 31,

비트 연산자: | 연산자(2/2)

11

5.3 (9/19)---[5-14.c ]#include<stdio.h>int main(void){ int num1=20, num2=16; int result1;

result1 = num1 | num2; printf(" | %d \n",result1);

return 0;}

11Thursday, March 31,

비트 연산자: ^ 연산자(1/2)

12

5.3 (10/19)!^

! XOR ! 1

0 ^ 0 00 ^ 1 11 ^ 0 11 ^ 1 0

12Thursday, March 31,

비트 연산자: ^ 연산자(2/2)

13

5.3 (11/19)---[5-15.c ]#include<stdio.h>int main(void){ int num1=20, num2=16; int result1;

result1 = num1 ^ num2; printf(" ^ %d \n",result1);

return 0;}

13Thursday, March 31,

비트 연산자: ~ 연산자(1/2)

14

5.3 (12/19)!~

! NOT !

~ 0 1~ 1 0

14Thursday, March 31,

비트 연산자: ~ 연산자(2/2)

15

5.3 (13/19)---[5-16.c ]#include<stdio.h>int main(void){ int num1=20; int result1;

result1 = ~num1; printf(" ~ %d \n",result1); // -21

return 0;}

15Thursday, March 31,

비트 연산자: 비트 이동 연산자(1/5)

16

5.3 (15/19)!

! << ( )•

! >> ( )•

16Thursday, March 31,

비트 연산자: 비트 이동 연산자(2/5)

17

5.3 (16/19)---[5-17.c ]

#include<stdio.h>int main(void){ int num1=10; int result1;

result1 = num1 << 2; printf(" << %d \n", result1);

return 0;}

17Thursday, March 31,

비트 연산자: 비트 이동 연산자(3/5)

18

5.3 (17/19)---[5-17.c ]

result1 = num1 << 2;

1 2

18Thursday, March 31,

비트 연산자: 비트 이동 연산자(4/5)

19

5.3 (18/19)---[5-18.c ]#include<stdio.h>int main(void){ int num1=10; int num2=-10;

int result1; int result2;

result1 = num1 >> 1; result2 = num2 >> 1;

printf(" >> %d \n", result1); printf(" >> %d \n", result2); return 0;}

19Thursday, March 31,

비트 연산자: 비트 이동 연산자(5/5)

20

5.3 (19/19)---[5-18.c ]

result1 = num1 >> 1;

result2 = num2 >> 1;

1 2

20Thursday, March 31,

음수만들기

21

5.3 (14/19)---[5-16.c ]

21 + (-21) = 0 이 됨을 확인할 수 있다.

21Thursday, March 31,

연습문제

22

(양수, 음수 표현 문제)가정: 1바이트를 가지고 데이터를 표현한다. 따라서 가장 왼쪽에 존재하는 비트는 부호비트가 된다.

문제 1) 양의 정수0100111100110011

문제 2) 음의 정수1010100111110000

22Thursday, March 31,

연습문제

23

(양수, 음수 표현 문제)가정: 1바이트를 가지고 데이터를 표현한다. 따라서 가장 왼쪽에 존재하는 비트는 부호비트가 된다.

문제 1) 양의 정수0100111100110011

문제 2) 음의 정수1010100111110000

= 26 + 23 + 22 + 21 + 20

= 25 + 24 + 21 + 20

= −(26 + 24 + 22 + 21 + 20)

= −24

23Thursday, March 31,

연습문제

24

문제 3) 입력 받은 음의 정수 값을 양의 정수 값으로 바꿔서 출력하는 프로그램을 작성해 보자. 단, 반드시 비트 단위 연산자를 사용해서 구현해야 한다.

include <stdio.h>

int main(void){ int n;

printf("type a negative integer: "); scanf("%d", &n); n = ???; n = ???;

printf("%d \n", n); return 0;}

24Thursday, March 31,

연습문제

25

문제 3) 입력 받은 음의 정수 값을 양의 정수 값으로 바꿔서 출력하는 프로그램을 작성해 보자. 단, 반드시 비트 단위 연산자를 사용해서 구현해야 한다.

include <stdio.h>

int main(void){ int n;

printf("type a negative integer: "); scanf("%d", &n); n = ~n; n = n+1;

printf("%d \n", n); return 0;}

25Thursday, March 31,

연습문제

26

문제 4) 입력 받은 값의 두 배를 계산해서 출력해 주는 프로그램을 *(곱셈) 연산이 아닌, 비트 이동(쉬프트) 연산 (<< 또는 >>)을 이용해서 구현해 보자.

주의) 입력값이 -2^30 - 1 (= -1073741825) 에서 2^30 (=1073741824) 를 벗어날 경우 전혀 다른 값이 출력됨에 주의 하라.

#include <stdio.h>

int main(void){ int n;

printf("type an integer value: "); scanf("%d", &n); n = ??? ; // double the value of n!

printf("%d \n", n); return 0;}

26Thursday, March 31,

연습문제

27

문제 4) 입력 받은 값의 두 배를 계산해서 출력해 주는 프로그램을 *(곱셈) 연산이 아닌, 비트 이동(쉬프트) 연산 (<< 또는 >>)을 이용해서 구현해 보자.

주의) 입력값이 -2^30 - 1 (= -1073741825) 에서 2^30 (=1073741824) 를 벗어날 경우 전혀 다른 값이 출력 됨에 주의 하라.

#include <stdio.h>

int main(void){ int n;

printf("type an integer value: "); scanf("%d", &n); n = n<<1; // double the value of n!

printf("%d \n", n); return 0;}

27Thursday, March 31,

연습문제

28

5-3) 연산 우선순위 관련 #include <stdio.h>

int main(void){ int x=3,y=5,z=3,k=2; int a; a = x < y || x < z && z < k; printf("The Result 1 a : %d \n", a); /* The operator '||' has a higherf priority than '&&'. x < y || x < z && z < k = x < y || (x < z && z < k) = 1 || (0 && 0) = 1 || 0 = 1 */ a = (x < y || x < z) && z < k; printf("The Result 2 a : %d \n", a); /* The part within parentheses has the priority. (x < y || x < z) && z < k = (1 || 0) && 0 = 1 && 0 = 0 */ return 0;}

28Thursday, March 31,

연습문제

29

5-4) 세 개의 입력 숫자 중 최대값 구하기

#include<stdio.h>int main(void){ int a,b,c; int temp,max; printf("Put an integer :"),scanf("%d",&a); printf("Put an integer :"),scanf("%d",&b); printf("Put an integer :"),scanf("%d",&c);

temp = (a>b)?a:b; max = (temp>c)?temp:c;

printf("The maximum : %d\n",max);

return 0;}

29Thursday, March 31,

연습문제

30

5-7) 네 자리의 2진수 입력을 받아 10진수 값을 출력하기.

#include <stdio.h>

int main(void){ int first, second, third, fourth, decimal;

int dec1, dec2; // for temporary decisions printf("--------Between 0000 and 1111, Convert to Demical Number-------\n"); printf("The First : "), scanf("%d",&first); printf("The Second : "), scanf("%d",&second); printf("The Third : "), scanf("%d",&third); printf("The Fourth : "), scanf("%d",&fourth);

decimal = (first*1)+(second*2)+(third*4)+(fourth*8);dec1 = first > 1 || second > 1 || third > 1 || fourth > 1;dec2 = first < 0 || second < 0 || third < 0 || fourth < 0;

(dec1 || dec2) ? printf("\n Wrong Input \n") : printf("\nThe binary input %d%d%d%d is converted to the decimal number %d.\n", fourth, third, second, first, decimal);

return 0;}

30Thursday, March 31,

연습문제

31

5-8) 시, 분, 초를 입력 받아 초로 계산하기.

include <stdio.h>

int main(void){ int hour,min,sec,total;

int dec1, dec2; printf("Put an hour :"); scanf("%d",&hour); printf("Put a minute :"); scanf("%d",&min); printf("Put a second :"); scanf("%d",&sec);

dec1 = hour >= 24 || min >= 60 || sec >= 60;dec2 = hour < 0 || min < 0 || sec < 0;

total = (hour * 3600) + (min * 60) + sec;

(dec1 || dec2) ? printf("\n Wrong Input \n") : printf("%d\n",total);

return 0;}

31Thursday, March 31,

연습문제

32

5-9) 파운드를 킬로그램으로 계산하기.

#include <stdio.h>

int main(void){ double pound = 0.45; double kg;

kg = 150*pound; printf("The 150lb of flour is %.2lf kg .\n",kg);}

32Thursday, March 31,