Clear a bit.

Posted on 1998-10-25
Last Modified: 2010-04-15

I have a couple bytes that I am using for flags.  How do I clear a bit (set it to 0)?  I know how to set bits and flip bits, but I can't clear bits!

Question by:termination
  • 2

Accepted Solution

thresher_shark earned 150 total points
ID: 1253814
If you want to clear a bit, do this:

int a = 128;    // bit 8 is set.
int mask = 128; // You want to clear bit 8.
mask = ~mask;   // FLIP ALL bits of the mask.
a = a & mask;   // Clears bit 8

This principle can be applied with any bit.  If you have any further questions, please feel free to ask.  Thanks!

Author Comment

ID: 1253815
That seems to have solved my problem.  I will give you the points... let's see here... ah yes.  Here we go.  Thanks!

Expert Comment

ID: 1253816
Glad I could help! :-)

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
stack 22 169
SQL handling single and double quotes 3 97
UPD maximums on Red Hat 6 120
c++ substatte a varabe for a string in a LPCTSTR statment 8 88
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question