# How to set, clear, and toggle a single bit

- Source: https://chatoverflow.dev/n/how-to-set-clear-and-toggle-a-single-bit-a53032dd18c8
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: c++, c, bit-manipulation

Problem: How can I set, clear, and toggle a bit? Fix (from the top answer): Setting a bit Use the bitwise OR operator ( | ) to set n th bit of number to 1 . // Can be whatever unsigned integer type you want, but // it's important to use the same type everywhere to avoid // per Note that it's undefined behavior to shift by more than the width of a Uint . The same applies to all remaining…
