C# flags vs sample enums [duplicate] - c#

This question already has answers here:
What does the [Flags] Enum Attribute mean in C#?
(14 answers)
Closed 6 years ago.
What is the difference between
[Flags]
public enum AnswerFlags
{
A = (1 << 2),
B = (1 << 1),
C = (1 << 0)
}
and
public enum AnswerFlags
{
A = 4,
B = 2,
C = 1
}
And why should i work with bits instead of integers?

The core difference between flags and classic enums is that
enum holds only one value: A or B or C
while
flags are meant to hold sets of values: A or B or C or AB or AC or BC or ABC.
You may want to read about it here:
MSDN

Related

C# How to separate string with out separator? [duplicate]

This question already has answers here:
Split String in C# without delimiter (sort of)
(2 answers)
Closed 2 years ago.
how to separate something from 83726473827 to
int[0] = 8;
int[1] = 3;
int[2] = 7;
etc...
I don't want use any separator
You can use Linq extension method Select with char.getNumericValue() like this:
using System.Linq;
string str = "83726473827";
int[] array = str.Select(c => (int)char.GetNumericValue(c)).ToArray();
foreach ( int value in array )
Console.WriteLine(value);
We take each char of the string and convert them to numeric value as integer, then we take the result as an array.
This code assumes that the string contains only digits else we need to take in consideration other cases.
Output
8
3
7
2
6
4
7
3
8
2
7

Flags and << operation on enums? C# [duplicate]

This question already has answers here:
Why use the Bitwise-Shift operator for values in a C enum definition?
(9 answers)
Closed 6 years ago.
Ok so I am new to C#, and for the life of me I cannot comprehend what exactly the below code (from a legacy project) is supposed to do:
[Flags]
public enum EAccountStatus
{
None = 0,
FreeServiceApproved = 1 << 0,
GovernmentAccount = 1 << 1,
PrivateOrganisationAccount = 1 << 2,
All = 8
}
What exactly does the << operator do here on the enums? Why do we need this?
Behind the scenes, the enumeration is actually an int.
<< is the Bitwise Left Shift Operator
An equivalent way of writing this code is :
[Flags]
public enum EAccountStatus
{
None = 0,
FreeServiceApproved = 1,
GovernmentAccount = 2,
PrivateOrganisationAccount = 4,
All = 8
}
Please note, that this enumeration has the Flag attribute
As stated in the msdn:
Use the FlagsAttribute custom attribute for an enumeration only if a
bitwise operation (AND, OR, EXCLUSIVE OR) is to be performed on a
numeric value.
This way, if you want to have multiple options set you can use:
var combined = EAccountStatus.FreeServiceApproved | EAccountStatus.GovernmentAccount
which is equivalent to:
00000001 // =1 - FreeServiceApproved
| 00000010 // =2 - GovernmentAccount
---------
00000011 //= 3 - FreeServiceApproved and GovernmentAccount
this SO thread has a rather good explanation about the flags attribute
<< is doing simply what does i.e. Shift left operation.
As far as why in an enum is concerned, its just a way of evaluating the expression as enums allow expressions (and evaluate them on compile time)

"ORing" two bytes together [duplicate]

This question already has answers here:
A clear, layman's explanation of the difference between | and || in c#?
(11 answers)
Closed 8 years ago.
I need to take two bytes and OR them together, a rather simple task. However, I do not know the proper syntax to OR the two bytes together.
byte First = 0x03;
byte Second = 0x15;
//Need to or Them
byte Result = First || Second; //This syntax does not work in C#
You need | Operator
byte Result = (byte)(First | Second);
|| is a logical or not bitwise so you need:
byte Result = (byte) (First | Second);

How to write "x >> 0" (from JavaScript) in C#? [duplicate]

This question already has answers here:
What does "somevar >> 0" mean?
(3 answers)
Closed 8 years ago.
Yet another question what does this operator do? How would I write that in C#:
data[id] = R >> 0;
data[id + 1] = G >> 0;
data[id + 2] = B >> 0;
I assume you're talking about the >> operator. It's a right shift operator that first (if necessary) converts the left argument to an integer and then shifts right by the indicated number of bits. Shifting by 0 bits leaves the number unchanged, so R >> 0 is a cute way of forcing R to an integer. It works like Math.floor(R) for non-negative values.
In C#, I believe that you can do the same thing with a cast: (int) R, etc.

Method similar to << and >> operators with is not loosing bits [duplicate]

This question already has answers here:
Is there a way to perform a circular bit shift in C#?
(5 answers)
Closed 9 years ago.
Example:
x = 0xF0AA, now when I do x = x << 8, I will get x == 0xAA00.
Right side (8 bits) of new value is filled with zeros. Is there a method int .NET Framework which can fill this bits with that bits from left side (with disapeared part)?
Result should be x == 0xAAF0.
What you want to do is called a circular shift. It's easy enough to emulate with two shifts and an or.
UInt32 RotateLeft(Uint32 n, int howManyBits) {
return n << howManyBits | n >> (32 - howManyBits);
}

Categories