What does "<<" operator mean in C# [duplicate] - c#

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

Related

What is the double question mark equals sign (??=) in C#? [duplicate]

This question already has answers here:
What do two question marks together mean in C#?
(19 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
I've been seeing statements like this a lot:
int? a = 5;
//...other code
a ??= 10;
What does the ??= mean in the second line? I've seen ?? used for null coalescing before, but I've never seen it together with an equals sign.
It's the same as this:
if (a == null) {
a = 10;
}
It's an operator introduced in C# 8.0: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator
Available in C# 8.0 and later, the null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. The ??= operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.

Can someone explain me what do these operators mean in C#? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Here are operators I need help with:
% (for example A%B)
!= (a%b != 0)
&(&&)
I'm very new to C# , so please try to explain me as simple as possible.
% Operator (C# Reference)
The % operator computes the remainder after dividing its first operand
by its second. All numeric types have predefined remainder operators.
User-defined types can overload the % operator (see operator). When a
binary operator is overloaded, the corresponding assignment operator,
if any, is also implicitly overloaded.
!= Operator (C# Reference)
The inequality operator (!=) returns false if its operands are equal,
true otherwise. Inequality operators are predefined for all types,
including string and object. User-defined types can overload the !=
operator.
For predefined value types, the inequality operator (!=) returns true
if the values of its operands are different, false otherwise. For
reference types other than string, != returns true if its two operands
refer to different objects. For the string type, != compares the
values of the strings.
User-defined value types can overload the != operator. So can
user-defined reference types, although by default != behaves as
described above for both predefined and user-defined reference types.
If != is overloaded, == must also be overloaded. Operations on
integral types are generally allowed on enumeration.
& Operator (C# Reference)
The & operator can function as either a unary or a binary operator.
The unary & operator returns the address of its operand (requires
unsafe context).
Binary & operators are predefined for the integral types and bool. For
integral types, & computes the logical bitwise AND of its operands.
For bool operands, & computes the logical AND of its operands; that
is, the result is true if and only if both its operands are true.
The & operator evaluates both operators regardless of the first one's
value.
&& Operator (C# Reference)
The conditional-AND operator (&&) performs a logical-AND of its bool
operands, but only evaluates its second operand if necessary.

math equation with ^ wrong variables used [duplicate]

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.

C# how to detect operators in string to calculate? [duplicate]

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.

What is ~ in C#? [duplicate]

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

Categories