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?