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);
}
Related
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
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)
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 8 years ago.
I have 2 input int values that need do be divided and sen back as a string with 6 decimals.
int a = 240
int b = 1440.
I want to divide them and send back a text string with 0,166667
I have tried many code examples but none have worked.
You need to convert at least one to a decimal value:
double result = (double)a / b;
or
decimal result = (decimal)a / b;
On "decimal vs. double" see THIS question.
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);
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.