1

i need to implement some kind of bit to status mapping and i don't know how to implement this thing - maybe i have a blockhead ;)

Okay so i have a status-code which can be max. 32 bit long for example 1001 0011 Each bit belongs to a special status code - string.

For example:

Bit 1: String Cancel
Bit 2: String Okay
Bit 3: String Save
....
Bit 32: String Next

Now i have to determine which bits are set and return all status code - strings which are set int the 32 bit long status.

I like to prefer some kind of dictionary or sumthin like that where i can store for each bit the corresponding status code.

Then i like to loop thru the 32 bit long status code and get the corresponding status code string out of the dictionary - store it in a string and return it after the 32 bit long status code has been looped through.

Somebody has an idea for that?

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Daniel W.
  • 449
  • 10
  • 29
  • 1
    try to use enums like in this question http://stackoverflow.com/questions/40211/how-to-compare-flags-in-c – wiero Mar 19 '12 at 11:57
  • @wiero: Post this as an answer, because that's the correct way to do this. – Daniel Hilgarth Mar 19 '12 at 11:59
  • @DanielHilgarth - Do you really have a million+ different status messages? 32-bits seems like its overkill. If you have more then 256 different status messages, at least my view on the subject, your doing something wrong. There are even ways to expand 2 bytes into more then 256 messages. – Security Hound Mar 19 '12 at 12:10
  • @Ramhound: I assume you wanted to address this question to the OP, not me... But with a 32 bit integer, you got 32 different status messages, if each bit represents one status message as defined by the OP: If bit 1 is set, it is status message 1. If bit 2 is set, it is status message 2 etc. Sure, you can have a combination of the bits, but still, you only have 32 different status messages. You only have 2^32 different combinations of these status messages... – Daniel Hilgarth Mar 19 '12 at 12:14
  • Hello, Daniel H. is on the right track. Each bit represents exactly one status - code / string / message - call it what ever you want. If two bits are set it does not result in a new status code for the sum of this two bits. It results in returning two status code strings. – Daniel W. Mar 19 '12 at 12:24

1 Answers1

3

you can use enums with flags attribute. Enum can be cast from and to int. Each enum value can be set so your status can look like this

[Flags]
enum Status { None = 0 , Cancel = 1 , Ok = 2 , Save = 4 ... } 

Flags attribute

Indicates that an enumeration can be treated as a bit field; that is, a set of flags.

you can test with

 if ((status & Status.Ok) == Status.Ok)
 {
 // Do something
 }
wiero
  • 2,176
  • 1
  • 19
  • 28
  • Note that `[Flags]` is not strictly required, only produces a nicer result from ToString(). – H H Mar 19 '12 at 12:18
  • I've also seen `... Cancel = 1 << 0, Ok = 1 << 1, Save = 1 << 2 ...` for those who dislike binary counting. – Jodrell Mar 19 '12 at 12:22
  • So i will have to "manually" check all 32 cases with an "if" statement? What if status has more than one bit set – Daniel W. Mar 19 '12 at 12:33
  • you can mix statuses and define Completed = Ok | Save . If it comes to handling different statuses you can create dictionary with status as key and handler as dictionary – wiero Mar 19 '12 at 12:40