This question already has answers here:
Ternary Operator in C#
(4 answers)
Closed 2 years ago.
encoded Data = (encoded Data | bit Shift Buffer)
This line:
j = j + 1 == key.Length ? 0 : j + 1;
could also be written as:
if ((j+1) == key.Length)
j = 0;
else
j = j+1;
Related
This question already has answers here:
How to update value in a List using LINQ
(9 answers)
Closed last year.
I have a list
List < double > newlist = new List<double>{5.0,1.1,2.1,5.1,0.2,5.2,7.5}
using System.Linq how to change all values between 5<i<6 to 5.5?
approach with linq - that replaces the existing List<double> with a new one
//[5.0,1.1,2.1,5.5,0.2,5.5,7.5]
newlist = newlist.Select(x => x > 5 && x < 6 ? 5.5d : x).ToList();
approach with a for loop - that one only changes the affected values
for (int i = 0; i < newlist.Count; i++)
{
if (newlist[i] > 5 && newlist[i] < 6)
{
newlist[i] = 5.5d;
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I recently stumbled upon a weird bug in one of my programs.
for (int i = 0; i < bitArray.Length; i++)
{
if (bitArray[i])
{
returnValue = returnValue + 2 ^ (7 - i);
}
}
In the snippet above, if returnValue = 0, by the time it reaches a set bit at i = 7, the resulting output is 2, which makes no sense (2^0 should be 1). However if I replace the code with:
returnValue = returnValue + Convert.ToInt32(Math.Pow(2,(7 - i)));
It is evaluated correctly to returnValue = 1
The problem is solved by using Math.Pow(), but I would very much like to know why it happens in the initial code.
In C# (and many other languages) the ^ operator is the boolean logical XOR.
See this document for more information about boolean operators in C#.
In addition to the other answers, it is probably simplest if you just use the C# left-shift operator <<, assuming bitArray.Length is less than or equal to 8:
for (int i = 0; i < bitArray.Length; i++)
{
if (bitArray[i])
{
returnValue = returnValue + 1 << (7 - i);
}
}
If returnValue is a float type and the intention is to allow negative powers of 2, then a slight change yields:
for (int i = 0; i < bitArray.Length; i++)
{
if (bitArray[i])
{
returnValue = returnValue + ((7-i) >= 0) ? 1 << (7 - i) : 1.0 / ((1 << (7 - i));
}
}
although in this case Math.Pow(2, 7 - i) is much cleaner.
for (int i = 0; i < bitArray.Length; i++)
{
if (bitArray[i])
{
returnValue = returnValue + Math.Pow(2, 7 - i);
}
}
As #IshaySela and #Adomas have said, ^ is a bollean logical XOR.
But in-order to do what you want to do without using math.pow() you can just make an if condition to check if the power is equal ZERO or not.
if (bitArray[i]) {
if ((7 - i) == 0){
//returnValue += 1;
}
else{
//returnValue += 2 ^ (7 - i);
}
}
This question already has an answer here:
c# parallel foreach loop finding index
(1 answer)
Closed 4 years ago.
I can't seem to get my Actions to execute in parallel. They are executing in sequence.. Could somebody help me?
Action<Type_arg1,Type_arg2>[] actions = new Action<Type_arg1,Type_arg2>[count];
for (int i = 0; i < count; i++)
actions[i] = new Action<Type_arg1,Type_arg2>(carryOutAction);
int iter = 0;
Parallel.ForEach(actions, (thisaction) =>
{
thisaction(arg1,arg2,iter);
iter++;
});
The code you have posted is actually running in parallel (or at least unordered, which is a good hint, that it is in parallel.)
PS> scriptcs
scriptcs (ctrl-c to exit or :help for help)
> var count = 20;
>
> Action[] actions = new Action[count];
>
> for (int i = 0; i < count; i++)
* {
* var contextValue = i;
* actions[i] = new Action(()=>Console.WriteLine(contextValue));
* }
>
> Parallel.ForEach(actions, (thisaction) =>
* {
* thisaction();
* });
0
6
7
8
9
11
12
14
16
17
18
19
10
13
3
1
4
2
5
15
{
"IsCompleted": true,
"LowestBreakIteration": null
}
This question already has answers here:
Is there an easy way to change a char in a string in C#?
(8 answers)
Replacing a char at a given index in string? [duplicate]
(7 answers)
how do I set a character at an index in a string in c#?
(7 answers)
Closed 5 years ago.
for (int i = 0; i < s.Length -1; i++)
{
temp = s[i]; // no errors here
s[i] = s[j]; //Property or indexer string.this[int] cannot be assigned to -- it is read only
s[j] = temp; //Property or indexer string.this[int] cannot be assigned to -- it is read only
}
I am dumb so please explain to me as if I was an 8 year old. Is this something you cannot do in C#? How do I fix this?
As others have already suggested s seems a string and thay are immutable so you have to convert s to a char array to do that. A possible way to do it would be:
char[] array = s.ToCharArray();
char temp = array[i];
array[i] = array[j];
array[j] = temp;
s = new string(array);
This question already has answers here:
Randomize a List<T>
(28 answers)
Closed 6 years ago.
i have some list with 5 numbers (exp. 1 2 3 4 5) i want to order them in random ordering each time (page refresh) examples: (2 4 3 1 5) (1 3 5 4 2) (5 1 2 3 4)... code in C#, Thanks
var loadcards = (from card in db.GameCards
select card).Take(5).ToList();
foreach (var item in loadcards)
{
Response.Write("<script>alert('" + item.cardId + "');</script>");
}
Something like this:
int[] RandomizeOrder(int[] input)
{
Random RNG = new Random();
bool[] cellMap = new bool[input.Length];
int[] output = new int[input.Length];
for(int i = 0; i < input.Length; i++)
{
int index = RNG.Next(input.Length)
while(cellMap[index)
index = RNG.Next(input.Length);
cellMap[index] = true;
output[index] = input[i];
}
return output;
}
PS: You can remove the cellMap if none of the values is 0