029. 비트 연산자 이해하기(|, &, ~, ^, <<, >>)029. 비트 연산자 이해하기(|, &, ~, ^, <<, >>)

Posted at 2010. 11. 12. 03:18 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>

main()
{
	unsigned char ch = 255, mask = 0x7F;

	printf( "%d \n",ch );				// 255
	printf( "%d \n",ch & mask);			// 127
	printf( "%d \n",(char)~ch );		// 0
	printf( "%d \n",ch ^ ch );			// 0
	printf( "%d \n",ch >> 1 );			// 127
	printf( "%d \n",mask << 1 );		// 254
}

관심있게 볼 부분은 mask이다. 이것은 IP Address와 Subnet Mask를 비트 마스크할 때와 같은 원리이다.

이러한 비트 연산자를 통해 빠른 연산을 할 수 있다.

물론 우리의 Desktop은 해당 사항은 별로 없을 것 같다.

아마 특수 형태 H/W, 예를 들면 방화벽에서는 빠른 연산이 필수 일 것이다.

//

028. 쉼표 연산자 이해하기(,)028. 쉼표 연산자 이해하기(,)

Posted at 2010. 11. 12. 03:12 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>

main()
{
	int x = 1, y = 2, max;

	max = x > y? x : y;

	printf( "max = %d, x = %d, y = %d", max, x, y );
}
//

027. 조건 연산자 이해하기(?:)027. 조건 연산자 이해하기(?:)

Posted at 2010. 11. 12. 03:10 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>

main()
{
	int x = 1;
	int y = 2;
	int max;

	max = x > y? x : y;
}

max = x > y? x : y > 5? y : x + y;

//