An enumeration with a value of 0x0001?

I have an enumeration declaration similar to this:

public enum Filter { a = 0x0001; b = 0x0002; } 

What does it mean? They use this to filter the array.

+4
source share
9 answers

It just means that if you do Filter-> a, you get 1. Filter-> b is 2.

The strange hexadecimal notation is just a notation.

EDIT: Since this is a “filter,” hexadecimal notation makes a little more sense.

By writing 0x1, you specify the following bit pattern:

 0000 0001 

And 0x2:

 0000 0010 

This gives a clearer idea of ​​how to use the filter.

So, for example, if you want to filter out data that have lower 2 bits, you can do:

 Filter->a | Filter->b 

which would match:

 0000 0011 

A hexadecimal notation makes the filter concept clearer (for some people). For example, it is relatively easy to determine the binary code 0x83F0 by looking at it, but much more complicated for 33776 (the same number in the database is 10).

+7
source

This means that they are integer values ​​assigned to these names. Enumerations are mainly called numbers. You can specify between the base enumeration type and the enumeration value.

For instance:

 public enum Colour { Red = 1, Blue = 2, Green = 3 } Colour green = (Colour) 3; int three = (int) Colour.Green; 

The default renaming type is int , but you can use any byte , sbyte , short , ushort , int , uint , long or ulong :

 public enum BigEnum : long { BigValue = 0x5000000000 // Couldn't fit this in an int } 
+10
source

It is not clear what it is that you find obscure, so let's discuss all of this:

The enumeration values ​​contain explicit numerical values. Each enumeration value is always represented as a numeric value for the underlying storage, but if you want to be sure that this is a numeric value, you must specify it.

The numbers are written in hexadecimal, this is often used when you want numeric values ​​to contain one bit of the masking kit. It is easier to see that only one bit is set when it is written as 0x8000, than when it is written as 32768.

In your example, this is not so obvious, since you have only two values, but for filtering by bits, each value represents one bit, so each value is two times larger than the previous one:

 public enum Filter { First = 0x0001, Second = 0x0002, Third = 0x0004, Fourth = 0x0008 } 

You can use this enumeration to filter out individual bits in a value:

 If ((num & Filter.First) != 0 && (num & Filter.Third) != 0) { Console.WriteLine("First and third bits are set."); } 
+4
source
+2
source

It can mean anything. We need to see more code to understand what it does.

0x001 is the number 1 . Each time you see 0x , it means that the programmer has entered a number in hexadecimal format.

+2
source

The main reason:

It is easier to read the hexadecimal notation when writing numbers, such as: "2 for power x".

To use the enumeration type as a bit flag, we need to increase the enum values ​​with the value 2 ... 1,2,4,8,16,32,64, etc. To keep it readable, hexadecimal notation is used.

Ex: 2 ^ 10 - 0x10000 in hexadecimal (pure and clean), but 65536 are written in the classic decimal notation ... The same goes for 0x200 (hexadecimal notation) and 512. (2 ^ 9)

+1
source

These seem to be bit masks. But their actual values ​​are 1 and 2 ...

You can assign values ​​to enumerations, such as:

 enum Example { a = 10, b = 23, c = 0x00FF } 

etc...

0
source

The use of hexadecimal notation like this usually indicates that there may be some bit manipulation. I often used this notation when I was doing this, for the reason that you asked this question - this notation is called from you and says: "Pay attention to me, I am important!"

0
source

Well, we can use infact integers, we can avoid any, since by default enum assigns 0 to its first member and an extra value to the next available member. Many developers use this to hit two targets with one bow.

  • Complicated code that makes it difficult to understand.
  • Faster performance since hexadecimal codes are closer to binary.

my opinion is that we still use why we are in the fourth generation language, just go to the binaries again

but its a pretty better way to play with bits and the encryption / decryption process.

0
source

All Articles