How to store a bit in C# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have the following code:
void MyBitMaker(int inputNumber)
{
Console.WriteLine(inputNumber << 1);
//TODO
//Bit bitHolder = inputNumber << 1;
//InitMico(ref bitHolder);
}
There is not a type for storing bits in C#. Is there any way to store bits in a variable.
I am programming a micro controller using C#, It uses the bits that are coming from a web service, fetches them and sends them to the controller to open, close, sleep and such things using an interface to change the input data to appropriate bits. My problem is that the micro has just 16 bytes memory in ram and I can not store more than two bytes, the micro should store the history of previous actions (this is extra, maybe, jargon). I have low space and need this small unit. I searched a lot and did not find anything. currently I am using bit operators using a class that I myself have implemented but it's not efficient at all because of using bit operations. I was wondering if someone has faced something that can help me.

The smallest addressable unit is a byte, so use that, or a bool which is still 8 bits, but only has two possible values to be set.
You can't reference a bit, so that would be useless anyway. If you need to address a specific position in a byte, you can pass along the position. Still, you can only change the bit's value by setting the entire byte.

I just want to store bits
Trivial solution: bool array.
If it really needed to store the bits in a compacted form, you can use the BitArray type, which uses an int array internally. You can index it similarly to a normal array:
var myBits = new BitArray(20); // initialize for 20 bits (1 int will be stored internally)
myBits[5] = true; // similar to this: myInt |= 1 << 5;

Related

Hashing a byte array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have an input byte array which is huge in size ( > 8000 bytes). I need to store this byte array in DB, and fetch later for further operation.
The problem is, this byte array column is unique. Assume that if I need to retrieve the entire table information on this byte array column. It becomes extremely costly operation when it comes to byte comparison. So thought of storing the Hash Value of this byte array just to make the comparison operation easier.
Just wanted to know if hash value which is generated from the byte array will be unique OR is there any other way to achieve this.
If the size (in bytes) of the hash is smaller than 8000, this is not possible. After all, there are 256 ^ 8000 possible inputs, so there must be at least that many possible outputs if the hash function needs to be unique.
As the default C# hashcode returns an int, which (depending on your architecture) is 32 or 64 bits (so 4 or 8 bytes), this is not even close to be possible with the default hash function. (Of course, you could write your own hash function, but that's quite pointless.)

Splitting a 200 bit hexadecimal bitmask [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a bitmask of 200 bit stored as a hexadecimal value.
In order to apply the & bit operator, I have to first convert the hex to an integer but 200 bit is too big for a uint64 so my question is : how do I split my bitmask in 4 different hexadecimal value without loosing data?
So that I can also split my 200 bit data and then compare every chunk of data with the corresponding chunk of bitmask without altering the result.
You can use the BigInteger from System.Numerics (it's a separate assembly):
BigInteger bi = BigInteger.Parse("01ABC000000000000000000000000000000000", System.Globalization.NumberStyles.HexNumber);
VERY IMPORTANT: prepend a "0" before the hex number! (because BigInteger.Parse("F", NumberStyles.HexNumber) == -1, while BigInteger.Parse("0F", NumberStyles.HexNumber) == 15
BigInteger implement the "classical" logical operators (&, |, ^)
Requires .NET 4.0
The most efficient way of achieving this is writing a class that can store and do binary operations on 200bits of data, have strings as input, etc.

C# 64 characters key [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I don't know C# but I have to do a update to a function that send a key to be encrypted.
I need to send a key with 64 characters, but the function that I have here only accepts 32 characters.
The function that initialize this process is:
Byte[] kkey = System.Text.Encoding.UTF8.GetBytes(sEncKey);
What I have to do for this accept a 64 characteres key?
The code you have included creates an array of type Byte which includes a number of elements matching whatever number of characters you provide in sEncKey.
It is my understanding that you are passing kkey to some function that will perform the encryption, and that this function only accepts a byte array of size 32. If this is correct, then you can't automatically increase the key size to 64 just by sending a longer key - it is the function you will have to change first, not it`s input.
Sound's like you're trying to increase security by using a larger key without replacing the lock first. Get a bigger lock first, then you can use a bigger key.
Also, if you need more help or guidance with this, you will need to provide info about the function, as well as some relevant code.

FxCop says I should return a generic list inteface instead of a byte array. Should I? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm writing a library and instead of returning a byte array from an EventArgs derivation, it says I should return something like IList or ReadOnlyCollection instead.
Normally I'd be all for this but most of the existing .NET Framework uses byte arrays as opposed to generic list interfaces.
So if I were to use IList then when accessing the eventargs, if a client wanted to call File.WriteAllBytes he or she would have to do using System.Linq; and call the ToArray extension method to get the IList in the form of an array of bytes. Of course there are other ways to do this but this is the most elegant and typical.
Clients of this library are always going to want things to be in terms of an array of bytes so that they interface nicely with the rest of the framework.
Also, optimization may come in to play here. There is potential for large amounts of bytes to be manipulated so having to recopy the entire list just to get it in the form of a byte array each time would likely slow things down.
Lastly, it's just plain unpleasant. If clients are always going to want a byte array, then why not just give it to them? Do framework design guidelines not apply in this situation? What would you do?
There is potential for large amounts of bytes to be manipulated so having to recopy the entire list just to get it in the form of a byte array each time would likely slow things down.
But that is precisely why it should not be a byte array. Suppose you do this:
byte[] x1 = GetByteArray();
x1[0] = 0;
byte[] x2 = GetByteArray();
Every time you call GetByteArray you have to create a new byte array. Why? Because someone might have changed the one you handed out last time to have different contents! By handing out a byte array you guarantee that you are going to have to reconstruct that byte array from scratch every single time.
By contrast, if you hand out a read only collection of bytes then you can hand out the same collection over and over again. You know it is not going to change.
Clients of this library are always going to want things to be in terms
of an array of bytes so that they interface nicely with the rest of
the framework.
There you have your answer - FxCop output is in most cases just helpful suggestions - not commands - if this particular one doesn't apply to you you can even turn it off.
The guidelines and recommendations offered by FxCop are not always applicable in every situation. You don't need to follow them, and in some situations you shouldn't.

Message parity check [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
Can someone help me out with implementing this sequence of calculations in C#?
This problem essentially describes a CRC with a 24-bit polynomial.
You can solve the problem simply using shift and XOR operations and a 24-bit (or larger) variable; no bigint required.
Recommended introductory reading:
http://en.wikipedia.org/wiki/Cyclic_redundancy_check
http://www.mathpages.com/home/kmath458.htm
http://www.ross.net/crc/download/crc_v3.txt
I took the opportunity to dabble with this. Interpreting the equations in the context of an implementation in software is tricky because there are many ways in which the polynomials can be mapped to data structures in memory - and, I assume, you'll want the solution you produce to seamlessly inter-operate with other implementations. In this context, it matters if your byte ordering is MSB or LSB first... it also matters if you align your bit-strings that aren't a multiple of 8 to the left or right. It is worth noting that the polynomials are denoted in ascending powers of X - whereas one might assume, because the leftmost bit in a byte has maximum index, that the leftmost bit should correspond to the maximum power of X - but that's not the convention in use.
Essentially, there are two very different approaches to calculating CRCs using generator polynomials. The first, and least efficient, is to use arbitrary precision arithmetic and modulo - as the posted extract suggests. A faster approach involves successive application of the polynomial and exclusive-or.
A implementation in Pascal can be found here: http://jetvision.de/sbs/adsb/crc.htm - translation to C# should prove trivial.
A more direct approach might involve encoding the message and the generator polynomial as System.Numerics.BigInteger objects (using C#/.Net 4.0) and calculate the parity bits exactly as the text above suggests - by finding the message modulo the polynomial - simply using the "%" operator on suitably encoded BigIntegers. The only challenge here is in converting your message and parity bits to/from a format suitable for your application.

Categories