35

I need to mask certain string values read from a database by setting a specific bit in an int value for each possible database value. For example, if the database returns the string "value1" then the bit in position 0 will need to be set to 1, but if the database returns "value2" then the bit in position 1 will need to be set to 1 instead.

How can I ensure each bit of an int is set to 0 originally and then turn on just the specified bit?

Michael Kingsmill
  • 1,815
  • 3
  • 22
  • 31
  • 5
    `intValue |= 1 << position;` –  Jun 16 '14 at 19:16
  • Read up on [bit wise operators](http://www.codeproject.com/Articles/1544/Bit-wise-operations-in-C) too. Always good to know – Quintium Jun 16 '14 at 19:17
  • 1
    @elgonzo, I like your solution better (since you don't need to figure out the hex number). I would upvote if it was an answer. – BradleyDotNET Jun 16 '14 at 19:18
  • possible duplicate of [How to unset a specific bit in an integer](http://stackoverflow.com/questions/8557105/how-to-unset-a-specific-bit-in-an-integer) – Thomas Weller Jun 16 '14 at 19:19
  • @BradleyDotNET, don't push an old and very sloooow man ;-) –  Jun 16 '14 at 19:20
  • There's a [similar question and answers](http://stackoverflow.com/questions/93744/most-common-c-sharp-bitwise-operations-on-enums) on SO already, it may provide a better insight for what you want to do. – LightBulb Jun 16 '14 at 19:23
  • possible duplicate of [Get a specific bit from byte](http://stackoverflow.com/questions/4854207/get-a-specific-bit-from-byte) – Thomas Weller Jun 16 '14 at 19:23
  • Does this answer your question? [How do you set, clear, and toggle a single bit?](https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit) – phuclv Feb 02 '21 at 04:08

4 Answers4

96

If you have an int value "intValue" and you want to set a specific bit at position "bitPosition", do something like:

intValue = intValue | (1 << bitPosition);

or shorter:

intValue |= 1 << bitPosition;


If you want to reset a bit (i.e, set it to zero), you can do this:

intValue &= ~(1 << bitPosition);

(The operator ~ reverses each bit in a value, thus ~(1 << bitPosition) will result in an int where every bit is 1 except the bit at the given bitPosition.)

11

To set everything to 0, AND the value with 0x00000000:

int startValue = initialValue & 0x00000000;
//Or much easier :)
int startValue = 0;

To then set the bit, you have to determine what number has just that bit set and OR it. For example, to set the last bit:

int finalValue = startValue | 0x00000001;

As @Magus points out, to unset a bit you do the exact opposite:

int finalValue = startValue & 0xFFFFFFFE;
//Or
int finalValue = startValue & ~(0x00000001);

The ~ operatior is bitwise not which flips every bit.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • 1
    Might also mention that an `&` with all `1`s and one `0` will set the specified bit to `0`, in case that's needed. – Magus Jun 16 '14 at 19:19
  • Hmm, complex way of setting it to 0, isn't it? – Thomas Weller Jun 16 '14 at 19:22
  • @ThomasW. Indeed, changed the code to enhance the "just assign to 0" option. It does illustrate the later code better though (IMO). – BradleyDotNET Jun 16 '14 at 19:24
  • to reset a value, you can xor it with itself – axelduch Jun 26 '14 at 14:20
  • @aduch, a very interesting approach of setting it to 0. It would certainly work, but might be the most confusing of the three. – BradleyDotNET Jun 26 '14 at 16:52
  • @BradleyDotNET Yes I understand it's confusing at first. It is also widely used in assembly to reset registers. – axelduch Jun 26 '14 at 17:02
  • @aduch, Good old assembly :) Sign of the apocalypse: When C# is written that includes assembly and/or assebmly-level optimizations – BradleyDotNET Jun 26 '14 at 17:04
  • 1
    @BradleyDotNET As long as your algorithm is optimized first, that makes sense to me. Without an optimized algorithm, bitwise operations are kind of useless. – axelduch Jun 26 '14 at 17:07
  • I just noticed (about 5 years later; go figure) that you had 0xFFFFFFF7 in your code example instead of 0xFFFFFFFE ( = ~0x00000001). Hope you don't mind that i "bug-fixed" your code example ;-) –  Apr 27 '19 at 21:43
4

so, this?

enum ConditionEnum 
{
    Aaa = 0,
    Bbb = 1,
    Ccc = 2,
}    

static void SetBit(ref int bitFlag, ConditionEnum condition, bool bitValue)
{
    int mask = 1 << (int)condition;
    if (bitValue)
        bitFlag |= mask;
    else
        bitFlag &= ~mask;
}
sangok
  • 41
  • 3
-2

Just provide a value, bit value and position. Note that you might be able to modify this to work for other types.

public static void SetBit(ref int value, bool bitval, int bitpos)
    {
        if (!bitval) value&=~(1<<bitpos); else value|=1<<bitpos;
    }
Holzkopf Bude
  • 155
  • 1
  • 1
  • 4