0

I see in C source code where the author define enum as follows

enum {
    BINDER_DEBUG_USER_ERROR             = 1U << 0,
    BINDER_DEBUG_FAILED_TRANSACTION     = 1U << 1,
    BINDER_DEBUG_DEAD_TRANSACTION       = 1U << 2,
    BINDER_DEBUG_OPEN_CLOSE             = 1U << 3,
};

What is the logical reason behind this?

alk
  • 69,737
  • 10
  • 105
  • 255
  • Making a series of masks using bitshift. – AntonH May 21 '14 at 10:57
  • 1
    You might like the question [Why use the Bitwise-Shift operator for values in a C enum definition?](http://stackoverflow.com/questions/3999922/why-use-the-bitwise-shift-operator-for-values-in-a-c-enum-definition/3999962#3999962). – pmg May 21 '14 at 11:02
  • I think main purpose is what ever author require he would set in enum.Any thing you can set. So basically author requirement is masking using above enum defined. – Jayesh Bhoi May 21 '14 at 11:03

2 Answers2

2

Its purpose is that every entry in the enum has one bit, so you can toggle each entry:

/* BINDER_DEBUG_USER_ERROR = 0b0000 */
enum foo {
    BINDER_DEBUG_USER_ERROR             = 1U << 0, // = 0b0001
    BINDER_DEBUG_FAILED_TRANSACTION     = 1U << 1, // = 0b0010
    BINDER_DEBUG_DEAD_TRANSACTION       = 1U << 2, // = 0b0100
    BINDER_DEBUG_OPEN_CLOSE             = 1U << 3, // = 0b1000
};

And now you can set/unset several of these flags in a variable by doing logical bit operations.

For example, you can set the FAILED_TRANSACTION and the OPEN_CLOSE by doing a logical OR:

enum foo x = BINDER_DEBUG_DEAD_TRANSACTION | BINDER_DEBUG_OPEN_CLOSE;
musicmatze
  • 4,124
  • 7
  • 33
  • 48
2

It makes it easier to see what bit is set to "1" but you would get the same result by assignment of a decimal value corresponding to bit you want to set to "1". To answer your question, there is a reason, and it's readibility.

zubergu
  • 3,646
  • 3
  • 25
  • 38