C: Bit mask question

rpowersau at gmail.com rpowersau at gmail.com
Thu Apr 30 14:11:03 UTC 2009


#define BITFIELD_A   (0x0F << 16)
#define MY_FIELD     (0x06 << 16)

// You want to save the bits your not interested while clearing the bits you
want to change.
tempRegister = register & ~BITFIELD_A;

// Then modify the bits you are interested in.
tempRegister |= MY_FIELD;

// And write it back to the register
register = tempRegister;


On Thu, Apr 30, 2009 at 7:19 AM, Bas Jacobs <basfriends at gmail.com> wrote:

> Hi folks,
>
> Consider a control register, which includes flags and bitfields. When
> writing such a register you often see constructions like register = flaga |
> flagb | flagc; etc. In a library those flags are then defined as #define
> flaga (1 << 6) and #define flagb (1 << 13) (for instance). In my library
> there is also a definition for a bitfield, which looks something like this:
> #define bitfieldA (0x0F << 16) for a 4bit bitfield located at bits 19 .. 16.
> Lets say that I want write 0x6 into this field, how do I do this (using the
> macro of course)?
>
> Hope someone can help me,
>
> Bas
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20090430/393f97d6/attachment.html>


More information about the ubuntu-users mailing list