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

Download and Read instruction in the given source file.

Fill in the blanks.

#include <stdio.h>
#include <stdint.h>

void dec2bin(unsigned int n);

void main() {
    
	uint8_t vals=0;
	
	// Assume 8 LEDs are connected to Digital Out pins of Port A (PA)
	// LED0 at PA[0] to LED7 at PA[7]
	uint8_t PA;
	
	// Initial Values of PA
	PA = 0b00001111;			
	printf("\n Initial PA: ");
	dec2bin(PA);
	
	
	// Exercise_1-1: Turning ON LED4 at PA[4] 
	PA |=  ________; 	    // turn ON LED4 
	printf("\n ex1-1: ");
	dec2bin(PA);
	
	//Exercise_1-2:  Read the bit at PA[4], 4th from LSB
	vals =  ________;	// read bit of PA[4]
	printf("\n ex1-2: %d\n", vals);
	
	
	// Exercise_2-1: Turn off LED4 at PA[4]
	PA &= ________;    	// turn off LED4  
	printf("\n ex2-1: ");
	dec2bin(PA);
	
	//Exercise_2-2:  Read the bit at PA[4], 4th from LSB
	vals =  ________;	// read bit of PA[4]
	printf("\n ex2-2: %d\n", vals);
	
	
	
	// Exercise_3-1: Turning ON LED5 and LED4,  at PA[5:4] 
	PA |=  ________;	// turn ON LED5 and LED4
	printf("\n ex3-1: ");
	dec2bin(PA);
	
	//Exercise_3-2:  Read bits LED5 and LED4,  at PA[5:4] 
	vals =  ________;	// read bits at PA[5:4] 
	printf("\n ex3-2: %d\n", vals);
	
	//Exercise_4-1:  SET  PA[0] and PA[7] 
	PA |= 0b10000001; 
	printf("\n ex4-1: ");
	dec2bin(PA);
	
	//Exercise_4-2:  CLEAR  PA[0] and PA[7] 
	PA &=  ________;
	printf("\n ex4-2: ");
	dec2bin(PA);
	
	
	//Exercise_5-1:  Toggle LED7 PA[7]
	printf("\n");    
	PA ^=  ________;
	printf("\n ex5-1: ");
	dec2bin(PA);
	
	printf("\n");    
	
}

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

Was this helpful?