Reputation: 141
I have been reviewing some existing code and am hung up on a line. I put all of the code that should be relevant to the question down below. Note, the variable evt
is a part of an enumeration type that contains 36 elements, I however only wrote 3 of the 36 for simplicity.
The line in question is:
events[evt / 32] |= (uint32_t)1 << (evt % 32);
I cant seem to wrap my head around how or why one would place evt / 32
in the array index call out. It seems to me that all but 4 product elements from the 36 value enumeration would be truncated. Please excuse my poor terminology as I am still a learning C. If anyone could help me understand what is going on here I would very much appreciate it.
Thank you for your time.
static volatile uint32_t events[8];
typdef enum event_e
{
EVT_1,
EVT_2,
EVT_36
}event_t;
void event_set(event_t evt)
{
events[evt / 32] |= (uint32_t)1 << (evt % 32);
}
Upvotes: 1
Views: 79
Reputation: 780984
events
is a bit map, and this is setting the bit that corresponds to the value of evt
to 1
. The first 32 bits go into events[0]
, the next 4 bits go into events[1]
. evt % 32
gets the remainder, which is the bit offset, and 1 << (evt % 32)
shifts 1
to that offset.
Upvotes: 6