We are using the DynamicLinq.cs class and are attempting to set the where clause of our query to check bitwise data values.
Enumeration example (has Flags Attribute on the enumeration):
None = 0
Flag1 = 1
Flag2 = 2
Flag3 = 4
What we specifically want to achieve is to return data excluding ‘Flag1’ values i.e. exclude resolved values of 1, 3, 5 & 7. I believe that in standard LINQ you can achieve this by using something like “& ~” but we just can’t seem to get the Dynamic Expression API to accept what we are doing, the parsing of the resultant query will always fail.
Does anyone know how to manipulate and work with bitwise enumerations in the Dynamic Expression API?
It looks like Dynamic LINQ does not support the bit-wise operations. You can get around by using this equivalent mathematical formula for the bitwise operations:
(a & 2^n == 0) <==> a % 2^(n+1) < 2^n
The Dynamic LINQ library does support the mod operator (%), so you can accomplish this:
Where("MyFlag % 2 < 1")
Basically how this works. Suppose you want to find all values that exclude Flag3. Since Flag3 = 4, that is 4 = 2^2, so n=2. The standard way of writing this would be (MyFlagValue & Flag3 == 0), that is, when you bitwise and with Flag3, you should get 0, representing the flag is not included.
This can also be expressed mathematically without using any bit-wise operators by grabbing the right-most 3 bits (by applying % 8), and checking if the value is less than 4, which would be the value if the 3-from-right bit is not set. So for example, with MyFlagValue=14
MyFlagValue = 14 = 0b1110
MyFlagValue % 8 = 6 = 0b0110
MyFlagValue % 8 < 4 = false ^ which means 3-from-right bit (Flag3) is set
Another example, where MyFlagValue=58:
MyFlagValue = 58 = 0b111010
MyFlagValue % 8 = 2 = 0b000010
MyFlagValue % 8 < 4 = true ^ which means 3-from-right bit (Flag3) is NOT set
So this can be expressed in a mathematical way that is understood by Dynamic LINQ to check if the third flag Flag3 is NOT set:
Where("MyFlagValue % 8 < 4")
A quick look through the dynamic linq source code shows no support for bitwise operations.
Related
Here is the F# code I have so far -
let parseChainId rawTransaction =
let tx = TransactionFactory.CreateTransaction rawTransaction
let chainIdWithAddend = (IntTypeDecoder().DecodeBigInteger tx.Signature.V - BigInteger 35) / BigInteger 2
let addend = ???
let chainId = chainIdWithAddend - addend
chainId
However, I'm sure this code is incomplete in some cases because I can't figure out how to determine the {0, 1} addend as specified in https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
If I could figure out how to determine the addend, then I believe the above algorithm for chain id extraction would be complete.
I don't know anything about Ethereum (or cryptology in general), but from reading the linked document, I don't think your logic is quite right. What you're calling the "addend" is defined as a parity value, which is always either 0 or 1. This is why the spec says that v = CHAIN_ID * 2 + 35 or v = CHAIN_ID * 2 + 36, which means that CHAIN_ID = (v - {35,36}) / 2.
This is a complete guess, but I think you can determine the parity simply by knowing whether v is even or odd. If it's odd, you want to subtract 35, and if it's even, you want to subtract 36, so that in either case you can divide the result exactly by 2.
If this is true, we can rewrite your code like this:
let v = IntTypeDecoder().DecodeBigInteger tx.Signature.V
let parity = (if v % 2 = 0 then 1 else 0)
let chainId = (v - BigInteger (35 + parity)) / BigInteger 2 // no remainder possible
I think you could also make the parity implicit by relying on F#'s integer division to discard the remainder:
let v = IntTypeDecoder().DecodeBigInteger tx.Signature.V
let chainId = (v - BigInteger 35) / BigInteger 2 // ignore remainder
Be careful about off-by-one errors, though. I haven't tested (or even tried to compile) any of this code.
Another caveat: All of the above only seems to apply in certain conditions, such as when nine elements have been hashed, instead of six. I assume you've already accounted for that.
I have a 3 element enum, it defines one of three contexts, for example red, green, or blue. This enum is used in a loop with millions of iterations, for example, pixel many. The fields are currently one int apart, the default. Given a desired production order of R,G,B,R,G,B..., I have currently resorted to checking if the value is currently B, thus assigning it to R, otherwise incrementing the value.
private enum CHANNEL_CONTEXT {RED, GREEN, BLUE} //here is a sample enum
//here is a sample loop with the relevant construct
CHANNEL_CONTEXT current = CHANNEL_CONTEXT.RED;
while(condition)
{
use current;
//...
if(current == CHANNEL_CONTEXT.BLUE)
current = CHANNEL_CONTEXT.RED
else
current+=1;
}
Is there a way to wrap a 3 field enum with a single operation, such that no branch is required to determine if it is time to wrap. I know modulus(%) fits the bill, but my motivation is a performance based one, and I'd break even at best with such an expensive operation(testing corroborated, but not exhaustively).
To put my agenda in perspective, if i had 256 relevant fields, I could create a byte based enum, and increment with impunity and intended overflow. Alas, I only have three, and I cant think of a way to manipulate any integral primitive in a way that three values are produced cyclically, using a lightweight ALU operation,(+,-,&,^,|,<<..etc). I also wouldn't have been able to think of a way to swap bits with no temporary using such operations, but there is a rarely practical but possible way to do so.
Can someone guide me to a way to distribute 3 integral enum values such that they are traversable periodically, with no branch required, and no division based operators used(like modulus)?
While it sounds very unlikely that you can beat x = (x + 1) % 3 you can try to use mapping table:
var map = new[]{1,2,0};
x = map[x];
You probably would need to wrap that in unsafe to remove boundary checks on array access.
If you really set on bit manipulation irrespective of readability of the code - the table of converting numbers you are interested in is small enough to build manually for each bit and then combine.
Truth table:
Source Result
Bit2 Bit1 Bit2 Bit1
0 0 0 1
0 1 1 0
1 0 0 0
1 1 x x
As you can see the values we are interested in only produce 2 non-zero bits so resulting expression will be very simple - one case for 1 for lower bit and one case for higher bit (assuming values never fall out of the range 0-2 (which is safe if this is the only transformation).
var b1 = (x & 1) >> 0; // extract lower bit 0
var b2 = (x & 2) >> 1; // extract higher bit 1
// only care of cases when pair of bits results in 1
var resultBit1 = 1 & (~b1 & ~b2); // 00 -> x1, other cases is 0
var resultBit2 = (1 & (b1 & ~b2)) << 1; // 01 -> 1x, other cases is 0
x = resultBit1 | resultBit2;
Or inlining all into one unreadable line:
x = 1 & ~(x | x >> 1) | 2 & (x & 1 & ~x >> 1) << 1;
I have a piece of C# code that another developer has copied from a blog post which is used to encode/obfuscate an integer. this code contains some syntax that i am unfamiliar with. It looks like it might be rounding down the result of the calculation to prevent it from exceeding the maximum size of an integer, if that is the case i am worried that two input values could potentially result in the same output. The obfuscated values need to be unique so i'm worried about using this code without understanding how it works
this is a simplified version of the code:
public static int DecodeNumber(int input)
{
return (input * PrimeInverse) & int.MaxValue;
}
so my question is
what is the meaning of the ampersand in this context and will this code produce an output that is unique to the input?
No, there is no "rounding" going on here. This is a sneaky way of truncating the most significant bit when multiplication results in overflow.
According to the documentation, int.MaxValue is 2,147,483,647, which is 0x7FFFFFFF in hex. Performing a bitwise AND with this value simply clears out the most significant bit.
Since the intention of the code is to use int.MaxValue for its binary pattern, rather than for its numeric value of the highest int that could be represented by Int32, I would recommend either using 0x7FFFFFFF constant explicitly, or computing it with ~ expressionL
return (input * PrimeInverse) & ~(1 << 31);
The ampersand is a bitwise AND operator. The numbers on the sides of this operator will be considered in binary format and a logic AND would be performed on the bits of the same significance.
The int.MaxValue equals 2,147,483,647. The result of this operation is explained as below:
operation:
a = x & int.MaxValue;
result:
if (x >= 0) {a = x;}
if (x < 0) {a = x + 2,147,483,648;}
if x is non-negative then a = x;
if x is negative, then a = x + 2,147,483,648;
EDIT :
Logical Operations:
Logical operations like AND, OR, XOR, etc are defined to work on Boolean (logical) values. Boolean variables can have either 1 or 0 as their values. The result of AND operation between two logical variables will be 1 if and only if both the variables are equal to 1. This is shown below:
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
Bitwise AND operator on numbers works using the basic AND operator. First the two numbers on the sides of operator are converted to binary format. If the number of digits in both numbers are not equal, zeros are added to the left side of the number with less number of digits to have the same number of digits for both numbers. Then the digits of the same significance are ANDed one by one the way explained above and the result of each operation will be written on the place with the same significance constructing the result. The bitwise and between 12 and 7 is shown below. 12 is demonstrated as 1100 in binary format and 7 is 0111.
12 = 0b1100
7 = 0b0111
12 & 7 = ?
1 1 0 0 &
0 1 1 1
----------
0 1 0 0 = 4
I ran into a bit of code similar to the code below and was just curious if someone could help me understand what it's doing?:
int flag = 5;
Console.WriteLine(0x0E & flag);
// 5 returns 4, 6 returns 4, 7 returns 6, 8 returns 8
Sandbox:
https://dotnetfiddle.net/NnLyvJ
This is the bitwise AND operator.
It performs an AND operation on the bits of a number.
A logical AND operation on two [boolean] values returns True if the two values are True; False otherwise.
A bitwise AND operation on two numbers returns a number from all the bits of the two numbers that are 1 (True) in both numbers.
Example:
5 = 101
4 = 100
AND = 100 = 4
Therefore, 5 & 4 = 4.
This logic is heavily used for storing flags, you just need to assign each flag a power of 2 (1, 2, 4, 8, etc) so that each flag is stored in a different bit of the flags number, and then you just need to do flags & FLAG_VALUE and if the flag is set, it'll return FLAG_VALUE, otherwise 0.
C# provides a "cleaner" way to do this using enums and the Flags attribute.
[Flags]
public enum MyFlags
{
Flag0 = 1 << 0, // using the bitwise shift operator to make it more readable
Flag1 = 1 << 1,
Flag2 = 1 << 2,
Flag3 = 1 << 3,
}
void a()
{
var flags = MyFlags.Flag0 | MyFlags.Flag1 | MyFlags.Flag3;
Console.WriteLine(Convert.ToString((int) flags, 2)); // prints the binary representation of flags, that is "1011" (in base 10 it's 11)
Console.WriteLine(flags); // as the enum has the Flags attribute, it prints "Flag0, Flag1, Flag3" instead of treating it as an invalid value and printing "11"
Console.WriteLine(flags.HasFlag(MyFlags.Flag1)); // the Flags attribute also provides the HasFlag function, which is syntactic sugar for doing "(flags & MyFlags.Flag1) != 0"
}
Excuse my bad english.
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)