What does '<<' do in a C# integer declaration? [duplicate] - c#

This question already has answers here:
C# Left Shift Operator
(4 answers)
Closed 7 years ago.
In C#, what does the << operator do when used to declare a variable?
int layer = 1 << 8;
I've tried looking up the operator, but only confirmed what I already understood. I'm not to sure how it is being applied in a way that is compatible with the integer.

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.

Related

additing the same numbers in int and byte, why results are different? c# [duplicate]

This question already has answers here:
Casting to byte in C# [duplicate]
(5 answers)
Closed 3 months ago.
When it converts to int implicitly - the result is obvious, but when it adds bytes - it`s different, why?
Your code just overflows byte — it supports integer values from 0 to 255. You should use at least short.

C# modulo operator [duplicate]

This question already has answers here:
Mod of negative number is melting my brain
(15 answers)
Modulo operation with negative numbers
(12 answers)
Closed 2 years ago.
Just recently I stumbled upon a problem using the (what I assumed to be modulo) operator %:
Basically (-1) % 5 returns -1 instead of 4, so % seems to return the remainder instead of the mathematically correct modulus (either that or Wolfram Alpha is bad at math).
My question is quite simple:
Is there a real modulo operator in C# that I don't know of? Or do I have to write my own code for that?
All results I can find in the internet only point to the % ...
There is no modulo operator in c#.
The % operator, is the remainder operator:https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#remainder-operator-
The remainder operator % computes the remainder after dividing its left-hand operand by its right-hand operand.

What does "|=" operator means in C#? [duplicate]

This question already has answers here:
What does |= (single pipe equal) and &=(single ampersand equal) mean
(3 answers)
Closed 7 years ago.
Just walked through referencesource.microsoft.com (line 217)
and discovered "|=" operator. What does it means? I assume it some sort of Boolean operation- but can't get what exactly it means. Does it mean "or equal", short form like "a= a|b" ?
|= is to = what += is to =. Just a shortcut to avoid the writing of a = a | b
its a bitwise inclusive OR and assignment operator.
For details refer
https://msdn.microsoft.com/en-us/library/ms173224.aspx
You are correct. This operator is known as the "OR assignment" operator and a |= b is equivalent to a = a | b. Here is the
documentation.

Difference between sbyte and SByte [duplicate]

This question already has answers here:
What is the difference between String and string in C#?
(66 answers)
Closed 7 years ago.
In what way are each intended to be used? Also the differences between them?
For an example:
sbyte myByte = 1;
SByte myByte = 1;
sbyte is an alias of System.SByte. There is no difference between the two. The guidelines state that it is recommended to use the alias sbyte instead of SByte. You can see the MSDN documentation here: https://msdn.microsoft.com/en-us/library/d86he86x.aspx

Why can't assign a constant string to a constant string type? [duplicate]

This question already has answers here:
Use int constant in attribute
(3 answers)
Closed 10 years ago.
As far as I know the concatenation of "H" + 'i' produce a constant string.
Am I or C# wrong ?
Code :
const string b = "H" + 'i';
// Error : The expression being assigned to 'b' must be constant
Does this count as a bug or a feature ?
This is a subtlety.
'i' is a char literal.
Adding it to a string involves a boxing conversion (to call string operator +(string x, object y), as specified in the spec), which is not a constant expression.
For more information, see my earlier answer.
Maybe the VS2010 error will help define it:
Constant initializer must be compile-time constant
so "H" + 'i' is a runtime value.

Categories

Resources