Does an 'increment bitwise shift operator' exist in C#?
Say I want to increment a number by a bitwise shift, i.e.
1, 2, 4, 8, 16, etc
Is there a way to condense the i = i << 1
below to something like increment operator (++)?
for (int i开发者_高级运维 = 1; i <= 256; i = i << 1)
{
Console.WriteLine(i);
}
You mean something like <<=.
See full list of C# operators
You can use <<=
for this. As in i <<= 1
.
Both of these are same. So you can use the bottom one.
--first one
for (int i = 1; i <= 256; i = i << 1)
{
Console.WriteLine(i);
}
--Second one
for (int i = 1; i <= 256; i <<= 1)
{
Console.WriteLine(i);
}
Seems like you are looking for the <<= operator.
So instead of: i = i << 1
You can write: i <<= 1
精彩评论