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?