This question already has answers here:
A clear, layman's explanation of the difference between | and || in c#?
(11 answers)
Closed 8 years ago.
I need to take two bytes and OR them together, a rather simple task. However, I do not know the proper syntax to OR the two bytes together.
byte First = 0x03;
byte Second = 0x15;
//Need to or Them
byte Result = First || Second; //This syntax does not work in C#
You need | Operator
byte Result = (byte)(First | Second);
|| is a logical or not bitwise so you need:
byte Result = (byte) (First | Second);
Related
This question already has answers here:
Split String in C# without delimiter (sort of)
(2 answers)
Closed 2 years ago.
how to separate something from 83726473827 to
int[0] = 8;
int[1] = 3;
int[2] = 7;
etc...
I don't want use any separator
You can use Linq extension method Select with char.getNumericValue() like this:
using System.Linq;
string str = "83726473827";
int[] array = str.Select(c => (int)char.GetNumericValue(c)).ToArray();
foreach ( int value in array )
Console.WriteLine(value);
We take each char of the string and convert them to numeric value as integer, then we take the result as an array.
This code assumes that the string contains only digits else we need to take in consideration other cases.
Output
8
3
7
2
6
4
7
3
8
2
7
This question already has answers here:
What does "somevar >> 0" mean?
(3 answers)
Closed 8 years ago.
Yet another question what does this operator do? How would I write that in C#:
data[id] = R >> 0;
data[id + 1] = G >> 0;
data[id + 2] = B >> 0;
I assume you're talking about the >> operator. It's a right shift operator that first (if necessary) converts the left argument to an integer and then shifts right by the indicated number of bits. Shifting by 0 bits leaves the number unchanged, so R >> 0 is a cute way of forcing R to an integer. It works like Math.floor(R) for non-negative values.
In C#, I believe that you can do the same thing with a cast: (int) R, etc.
This question already has answers here:
Is there a way to perform a circular bit shift in C#?
(5 answers)
Closed 9 years ago.
Example:
x = 0xF0AA, now when I do x = x << 8, I will get x == 0xAA00.
Right side (8 bits) of new value is filled with zeros. Is there a method int .NET Framework which can fill this bits with that bits from left side (with disapeared part)?
Result should be x == 0xAAF0.
What you want to do is called a circular shift. It's easy enough to emulate with two shifts and an or.
UInt32 RotateLeft(Uint32 n, int howManyBits) {
return n << howManyBits | n >> (32 - howManyBits);
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
In one of the interview, i got asked the following question but i am not sure what is that, Please help me to understand about the question
Use C# to turn an 4th and 7th bits of a said byte myFlag in which write most bit is first bit.
Each byte has 8 bits, which are either on (1) or off (0). So you'd want to turn on the specified bits, using the bitwise operators.
how about ORing with 72 (01001000) ?
myFlag = myFlag | 72;
Assuming my interpretation is correct you are looking to use bit-wise operators to solve the problem. In order to ensure a particular bit is on use | with the bits set that you want set.
myFlag = myFlag | 0b00010010
Or equivalently
myFlag |= 18
If it helps to see the string of bytes, then you can use the Convert class to convert integers to bit strings and reverse to help visualise the effects of the bitwise OR. Below is a sample that creates a toggledOnFlag that has the bits toggled on. You could OR with the other bit string to switch them off.
var toggleBitsOn = "01001000";
var toggleBitsOff = "10110111";
var toggle = Convert.ToInt32(toggleBitsOn, 2);
var toggledOnFlag = myFlag | toggle;
Console.WriteLine(Convert.ToString(toggledOnFlag, 2));
You didn't specify how to declare "myFlag", but this should be what you're looking for.
[Flags]
enum BitFlags : byte
{
One = ( 1 << 0 ),
Two = ( 1 << 1 ),
Three = ( 1 << 2 ),
Four = ( 1 << 3 ),
Five = ( 1 << 4 ),
Six = ( 1 << 5 ),
Seven = ( 1 << 6 ),
Eight = ( 1 << 7 )
}
static void Main(string[] args)
{
BitFlags myFlag = BitFlags.Four | BitFlags.Seven;
Console.WriteLine( Convert.ToString( ( byte ) myFlag, 2 ) );
}
Edit: Updated for C# clarity and used "Flags" attribute, which is probably more along the lines of what the interviewer was looking for.
This question already has answers here:
C# XOR on two byte variables will not compile without a cast [duplicate]
(7 answers)
Closed 4 years ago.
string cetvrtadva = textBox76.Text.Substring(12, 2);
byte cetvrtadvaa = byte.Parse(cetvrtadva,
System.Globalization.NumberStyles.AllowHexSpecifier);
byte[] xor = { 0x09 ^ 0x45 ^ 0x3a ^ 0x08 ^ cetvrtadvaa };
Why i cant add byte to byte array?
Error: cannot implicitly convert int to byte.
The issue is that you are not putting a byte into the array, but an int.
It is true that cetvrtadvaa is a byte, but all the other numbers (0x09, 0x45, etc.) you use in the xor operation are ints. Therefore, before ^ is actually done, cetvrtadvaa is converted into an int so that both sides of the operation have the same type.
Therefore, you need to explicitly cast the the result back:
byte[] xor = { (byte)(0x09 ^ 0x45 ^ 0x3a ^ 0x08 ^ cetvrtadvaa) };