How to Set and Clear bits in C | Set and Clear the bits in C | Bitwise Operators in C | Toggle bits

Опубликовано: 21 Июль 2026
на канале: Know about Embedded System
536
14

In C programming, bits are set and cleared using bitwise operators. The key is creating a bitmask with the left shift operator () and then applying the appropriate operation: bitwise OR (|) to set a bit, and bitwise AND (&) with the bitwise NOT (~) to clear a bit.

For best practice and portability, use unsigned integer types like unsigned int or uint32_t (from stdint.h) for bitwise operations to avoid undefined behaviour associated with signed integers.

Clearing a bit forces it to 0, regardless of its current value. This is achieved using the bitwise AND (&) and bitwise NOT (~) operators.

Setting a Bit
Setting a bit forces it to 1, regardless of its current value. This is achieved using the bitwise OR (|) operator.


If you have any questions please write to us
email: [email protected],
[email protected]

for courses related to Embedded System, Automotive and RTOS
contact : 8073162262

👍 Like | 💬 Comment | 🔔 Subscribe for more C & Embedded tutorials

#setbit
#clearbit
#bitmanipulation
#bitwiseoperators
#cprogramming
#embeddedc
#embeddedprogramming
#microcontroller
#lowlevelprogramming
#registerprogramming
#driverdevelopment
‪@KnowaboutEmbeddedSystem‬