This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the tilde (~) in a C# enumeration?
What does the tilde mean in an expression?
I have download a CRC code snippet from the internet in C# having these lines:
byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);
or
public static UInt32 Compute(byte[] buffer)
{
return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
}
The ~ operator performs a bitwise complement operation on its operand, which has the effect of reversing each bit. Bitwise complement operators are predefined for int, uint, long, and ulong.
From: http://msdn.microsoft.com/en-us/library/d2bd4x66.aspx
And although it is not used this way in the example you have shown, the ~ character is also used to declare a destructor in C#.
Take a look at MSDN: ~ Operator
Related
This question already has answers here:
MemoryStream: why convert to byte after readByte
(3 answers)
Closed 7 years ago.
I spent some time on research System.IO namespace and after that I've found some interesting place for me. Stream class contains method with name ReadByte and definition like that:
public virtual int ReadByte()
So actually my question is why method called ReadByte and returns int?
What is the purpose of naming like that?
ReadByte returns
The unsigned byte cast to an Int32, or -1 if at the end of the stream.
Such approach (having a special value which does not collide with any possible byte value) leads to use of a an integral type which "extends" byte.
In principle, types like short would work as well, but it is much more natural and conventional to use int.
This question already has answers here:
operator << in c# [duplicate]
(5 answers)
Closed 7 years ago.
For example, if I write Console.WriteLine(1<<2<<2+1); in C# console application, the output will be 32
Can you say me why? What does this "<<" operator mean?
Where can I read more about it? I google but couldn't find it
From MSDN:
The left-shift operator (<<) shifts its first operand left by the number of bits specified by its second operand. The type of the second operand must be an int or a type that has a predefined implicit numeric conversion to int.
https://msdn.microsoft.com/en-us/library/a1sway8w.aspx
The number 32 is returned in this case because the addition operator has precedence over the ASHL (<<) operator, but the leftmost ASHL operators are applied first. The expression is evaluated as follows:
1<<2<<2+1
((1<<2)<<(2+1))
((1<<2)<<3)
(4<<3)
32
This question already has answers here:
Is there an exponent operator in C#?
(9 answers)
Closed 8 years ago.
I'm doing this equation
double1 * ((double2/double3) ^ 2.333)
getting error
operator '^' cannot be used on type double.
what variable should I use instead of double?
Use Math.Pow(value, exponent) instead.
Math.Pow(3,2) outputs 9
The operator you attempted to use (^) cannot be used for this purpose. It is a logical XOR and should be used for bitwise exclusive OR operations.
This question already has answers here:
Is there a string math evaluator in .NET?
(18 answers)
Closed 9 years ago.
Say a program receives an input string "8*10+6/2" and should output 83, in this case. how to handle the operator?
I can chop the string into individual strings, then detect whether it is a number or operator. If it is an operator I can convert it to int. But I have no idea how to handle the operator so that the calculation works.
You could use the DataTable.Compute-"trick":
double result = (double)new DataTable().Compute("8*10+6/2", null);
The following arithmetic operators are supported in expressions:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
More informations in: DataColumn.Expression at Expression Syntax.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Equivalent of Java triple shift operators (>>> and <<<) in C#?
Java has operator >>> and <<< which are a bit different then >> and << - can anyone give me its equivalent in C# ?
The simplest (or at least most logical) equivalent is effectively an unchecked cast to the equivalent unsigned type, followed by a normal shift and then potentially a cast back again:
// To perform int result = x >>> 5;
int x = -10;
uint u = unchecked ((uint) x);
u = u >> 5;
int result = unchecked ((int) u);
(The unchecked part is only relevant if you're otherwise in a checked context, of course.)
In my experience, times where you normally want to use >>> in Java, you'd just use unsigned types to start with in C#.
There is no c# equivalent, if you use an unsigned value on the left, >> in c# will perform the same function as >>> in java.
You therefore need to cast to get the desired effect.
Java has >>> (I don' think there is a <<< operator) which is the unsigned right shift operator that is not present in c#. It is there in java as java has not unsigned data types. In c# just use an unsigned type with >> operator.
>>> is an unsigned shift operations in Java.
They don't have an equivalent in C# because C# supports unsigned integers and hence you can just shift on those.