Bitwise Operation

Lecture PPT

Online Lesson

코딩도장 핵심요약: 비트연산자 사용하기 핵심요약

Bitwise Operation in C

자료형과 메모리 주소를 바이트 단위로 구분하여 사용하였습니다. 비트 연산자는 바이트 단위보다 더 작은 비트 단위로 연산하는 연산자입니다.

Example

	PA=PA | (1<<5);  	// set PA5 (as High) and mask others
	PA=PA & ~(1<<5);  	// reset PA5 (as LOW) and mask others
	bit = PA & (1<<5);  // check the bit5. bit=1 if  PA5 is 1


Set flag: (플래그 |= 마스크)

a |= (1 << k)

Clear flag (플래그 &= ~마스크)

Example:

unsigned char flag = 7;    // 7: 0000 0111
flag &= ~2;    

0000 0010
_________ ~
1111 1101

Toggle flag (플래그 ^= 마스크)

a ^= 1<<k

Read a bit

(Method 1) bit = a & (1<<k) // Shift ‘bit 1’ left by k starting from LSB

(Method 2) bit = (a >>k) & (1) // Shift target ‘bit right by k

Example:

Read multiple bits

Tip: Use Macro

#define BIT_SET(REG, BIT)		((REG) |= 1<< (BIT))
#define BIT_CLEAR(REG, BIT)		((REG) &= ~1<<(BIT))
#define BIT_READ(REG, BIT)		((REG)>>BIT & (1))
#define BITS_CLEAR(REG, BIT,NUM)	((REG) &= ~((0x1<< NUM)-1)<<(BIT))
#define BITS_SET(REG, BIT,NUM)		((REG) |= NUM<< (BIT))


Exercise

Exercise 1

What will be the output ?

#include <stdio.h>
 
int main()
{
    unsigned char num1 = 1;    // 0000 0001
    unsigned char num2 = 3;    // 0000 0011
    unsigned char num3 = 162;    // 162: 1010 0010
    unsigned char num4;
    num4 = ~num3;

 
    printf("%d\n", num1 & num2);    
    printf("%d\n", num1 | num2);    
    printf("%d\n", num1 ^ num2);    
    printf("%u\n", num4);    // 93: 0101 1101: num1의 비트 값을 뒤집음
 
    return 0;
}

Exercise 2

What will be the output?

#include <stdio.h> 

int main()
{
    unsigned char num1 = 4;    // 4: 0000 0100
    unsigned char num2 = 4;    // 4: 0000 0100
    unsigned char num3 = 4;    // 4: 0000 0100
    unsigned char num4 = 4;    // 4: 0000 0100
    unsigned char num5 = 4;    // 4: 0000 0100
 
    num1 &= 5;     // 5(0000 0101) AND 연산 후 할당
    num2 |= 2;     // 2(0000 0010) OR 연산 후 할당
    num3 ^= 3;     // 3(0000 0011) XOR 연산 후 할당
    num4 <<= 2;    // 비트를 왼쪽으로 2번 이동한 후 할당
    num5 >>= 2;    // 비트를 오른쪽으로 2번 이동한 후 할당
 
    printf("%u\n", num1);    
    printf("%u\n", num2);    
    printf("%u\n", num3);    
    printf("%u\n", num4);    
    printf("%u\n", num5);    
 
    return 0;
}

Exercise 3

Fill in the blanks.

//Exercise_1: Turning ON LEDs of Port A(PA)
//Read PA6, 6th from LSB
PA = b00001111;                 // LED0 is LSB, Set to turn on LED
uint8_t bits = PA & ________;	// check bit of a6


// Exercise_2: Turning ON LEDs of Port A(PA)
// assume 8 LEDs are connected to Digital Out pins of PA
PA = b00001111;         // LED0 is LSB, Set to turn on LED
PA |= ____________; 	// turn ON LED4 
PA |= ____________; 	// turn ON LED4 and LED5


// Exercise_3: Turning off LEDs of Port A(PA)
PA = b00001111;         // LED0 is LSB, Set to turn on LED
PA &= ~(________);      // turn off LED2 

Exercise 4

Download and Read instruction in the given sourcefile.

Apply bitwise operations (Set HIGH,Toggle, Reset etc) as instructed.

#include <stdio.h>

void main() {

unsigned int a = 118;
printf("\n118 as binary = ");
dec2bin(a);

//read 8th bit of a :   a[7]
unsigned int bit_check = a & (1 << 7);
printf("\nresult1 = %d", bit_check);

// set 8th (a[7])  bit as HIGH
// YOUR CODE GOES HERE
printf("\nresult2 = ");
dec2bin(a);

}

/////////////////////////
void dec2bin(unsigned int n) {
	unsigned int a = 0x80;
	for (int i = 0; i < 8; i++) {
	if ((a & n) == a)
		printf("1");
	else
		printf("0");
	a = a>> 1;
	}
}

Last updated