Bit set, reset, toggle and masking in Microcontrollers

Well I would like your concepts on bit set and reset operations to be clear. No matter which language you use be it assembly or embedded c, you will always require to set and reset register bits.

Now whether you want to initialize some peripheral or poll something you will require these three operations.

Bit set operation

This means making a particular bit in an register as 1 or setting it. Now let us see this property of logical OR.

x OR 1 = 1

So whatever be the value of x if you OR it with 1 the result will be 1. This is what we want right? I’ll be taking one example after explaining bit reset.

Bit reset operation

This means that you want to make a particular bit of a register 0 or reset it. We make use of the property of logical AND:

x AND 0 = 0

So whatever be the value of x if you AND it with 0 the result is 0. This is what we wanted so bit reset is done by logical AND operation.

Masking

Often you are interested only in a particular bits of a register. This may be the input of some sensor or something else. Now you want to use masking in this scenario. So masking means you reset all the bits that are not required and let the bits under consideration as it is. Again we make use of logical AND properties.

x AND 0 = 0

x AND 1 = x

So you make the masking pattern by making the bits you want as 1 and the ones you don’t want  as 0. Convert this value to hex and logical AND that mask value with the register.

Bit toggle

Bit toggle is also useful. So for this we make use of the property of ex-or as follows.

x ex-or 1 = complement of x or x-bar

Thus if you ex-or the bit with 1 the bit will be toggled.

Example

130620141602

R &= 0x2081 // This is the masking operation in embedded c

R |= 0x2081 // This will set the bits 0,7,13

R ^= 0x2081 // This will toggle bits 0,7,13

So as you can see this example covers masking. Similarly you need to make the bit patterns for bit set, reset and toggle operation.

If you want more examples comment here. Please like the posts if you like what I have written. And do subscribe to get notifications of my new posts.

Advertisements

4 thoughts on “Bit set, reset, toggle and masking in Microcontrollers

  1. HI, I think there are some typos in the last sentence on masking:

    You wrote: “Convert this value to hex and that you and with the register value.”

    I suppose you meant to write:

    “Convert this value to hex and AND that with the register value.”

    Keep up the good work!

  2. Hmm. Standard “bit clear” in C is:
    dest &= ~(bitmask);
    Which you don’t mention, and is especially confusing to people who haven’t seen it before. (“What does the &=~ operator do??”)

    You could write a lot more. a “modern” description of bit operations should mention C bitfields, ARM bitbanding, and the various optimizations that HW vendors have provided for dealing with individual bits in GPIO port registers (especially set/clear/toggle registers.)

    • Thank you for the suggestion. I know I have not done justice to the topic. Given the amount of time I get for this, I tried to do some amount of justice. Maybe I’ll expound on this topic later and write an exhaustive post when I get vacation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s