Bitwise Operation
Lecture PPT
Online Lesson
์ฝ๋ฉ๋์ฅ ํต์ฌ์์ฝ: ๋นํธ์ฐ์ฐ์ ์ฌ์ฉํ๊ธฐ ํต์ฌ์์ฝ
Bitwise Operation in C
์๋ฃํ๊ณผ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ๋ฐ์ดํธ ๋จ์๋ก ๊ตฌ๋ถํ์ฌ ์ฌ์ฉํ์์ต๋๋ค. ๋นํธ ์ฐ์ฐ์๋ ๋ฐ์ดํธ ๋จ์๋ณด๋ค ๋ ์์ ๋นํธ ๋จ์๋ก ์ฐ์ฐํ๋ ์ฐ์ฐ์์ ๋๋ค.


Example
Set flag: (ํ๋๊ทธ |= ๋ง์คํฌ)
a |= (1 << k)

Clear flag (ํ๋๊ทธ &= ~๋ง์คํฌ)
Example:

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
Exercise
Exercise 1
What will be the output ?
Exercise 2
What will be the output?
Exercise 3
Download and Read instruction in the given source file.
Fill in the blanks.
Last updated
Was this helpful?