Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 35549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:15:12+00:00 2026-05-10T14:15:12+00:00

How can I set, clear, and toggle a bit?

  • 0

How can I set, clear, and toggle a bit?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:15:12+00:00Added an answer on May 10, 2026 at 2:15 pm

    Setting a bit

    Use the bitwise OR operator (|) to set nth 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 // performance issues caused by mixing integer types. typedef unsigned long Uint;  // In C++, this can be template. // In C11, you can make it generic with _Generic, or with macros prior to C11. inline Uint bit_set(Uint number, Uint n) {     return number | ((Uint)1 << n); } 

    Note that it’s undefined behavior to shift by more than the width of a Uint. The same applies to all remaining examples.

    Clearing a bit

    Use the bitwise AND operator (&) to set the nth bit of number to 0.

    inline Uint bit_clear(Uint number, Uint n) {     return number & ~((Uint)1 << n); } 

    You must invert the bit string with the bitwise NOT operator (~), then AND it.

    Toggling a bit

    Use the bitwise XOR operator (^) to toggle the nth bit of number.

    inline Uint bit_toggle(Uint number, Uint n) {     return number ^ ((Uint)1 << n); } 

    Checking a bit

    You didn’t ask for this, but I might as well add it.

    To check a bit, shift number n to the right, then bitwise AND it:

    // bool requires #include <stdbool.h> prior to C23 inline bool bit_check(Uint number, Uint n) {     return (number >> n) & (Uint)1; } 

    Changing the nth bit to x

    There are alternatives with worse codegen, but the best way is to clear the bit like in bit_clear, then set the bit to value, similar to bit_set.

    inline Uint bit_set_to(Uint number, Uint n, bool x) {     return (number & ~((Uint)1 << n)) | ((Uint)x << n); } 

    All solutions have been tested to provide optimal codegen with GCC and clang. See https://godbolt.org/z/Wfzh8xsjW.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can very seldom use output redirection to replace the… May 11, 2026 at 11:33 pm
  • Editorial Team
    Editorial Team added an answer If you already use Ant, then your best bet is… May 11, 2026 at 11:33 pm
  • Editorial Team
    Editorial Team added an answer First of all, thanks to you guys who answered! =)… May 11, 2026 at 11:33 pm

Related Questions

As per this website, I wish to represent a Maze with a 2 dimensional
I am developing CDC driver for USB device and I stumble on some problem.
I searched for a few days now but didn't find a solution. So here
I'm replacing an old web application with a new one, with different structure. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.