2

If i take a uint value = 2921803 (0x2C954B), which is really a 4 byte package (4B 95 2C 00) and i want to get the 16 least significant bits of the byte version of it using bitarray, how would i go about it?

This is how i am trying to do it:

byte[] bytes = BitConverter.GetBytes(value); //4B 95 2C 00 - bytes are moved around
BitArray bitArray = new BitArray(bytes); //entry [0] shows value for 1101 0010 (bits are reversed)

At this point, i am all turned around. I did try this:

byte[] bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
BitArray bitArray = new BitArray(bytes);

Which gave me all the bits but completely reversed, reading from [31] to [0]. ultimately, i'm expecting/hoping to get 19349 (4B 95) as my answer.

This is how i was hoping to implement the function:

private uint GetValue(uint value, int bitsToGrab, int bitsToMoveOver)
{
 byte[] bytes = BitConverter.GetBytes(value);
 BitArray bitArray = new BitArray(bytes);
 uint outputMask = (uint)(1 << (bitsToGrab - 1));
 //now that i have all the bits, i can offset, and grab the ones i want
 for (int i = bitsToMoveOver; i < bitsToGrab; i++)
 {
 if ((Convert.ToByte(bitArray[i]) & 1) > 0)
 {
 outputVal |= outputMask;
 }
 outputMask >>= 1;
 }
}
asked Aug 17, 2011 at 20:26
2
  • 1
    You want to get 0x4B95 from 0x2C954B ? Commented Aug 17, 2011 at 20:31
  • This is ultimately what i came up with. If anyone would like to improve upon it, i welcome it. Commented Aug 18, 2011 at 13:27

2 Answers 2

3

The 16 least significant bits of 0x2C954B are 0x954B. You can get that as follows:

int value = 0x2C954B;
int result = value & 0xFFFF;
// result == 0x954B

If you want 0x4B95 then you can get that as follows:

int result = ((value & 0xFF) << 8) | ((value >> 8) & 0xFF);
// result == 0x4B95

Try this:

uint value = 0x002C954Bu;
int reversed = Reverse((int)value);
// reversed == 0x4B952C00;
int result = Extract(reversed, 16, 16);
// result == 0x4B95

with

int Extract(int value, int offset, int length)
{
 return (value >> offset) & ((1 << length) - 1);
}
int Reverse(int value)
{
 return ((value >> 24) & 0xFF) | ((value >> 8) & 0xFF00) |
 ((value & 0xFF00) << 8) | ((value & 0xFF) << 24);
}
answered Aug 17, 2011 at 20:30
Sign up to request clarification or add additional context in comments.

6 Comments

this works, but the primary problem is that the function has to be modifiable. By that I mean, I need to know how many bits to offset from the beginning, and how many i need to grab. I cannot guarantee that they will end up on an 8 bit boundary, either. (the 4B95 Result is the one that i need)
@Jason : please update an question with this details, it would be great if you can provide method signature which are you expect, or describe more detailed which kind of flexibility are you need
@Jason : approach used by dtb much faster rather than using BitArray and what is more important - you've a chance to understand bitwise operations better
@dtb - This is much closer, but the offset needs to be 0. The reason is that although my presented as uints, they are really bytes so when i do the bitconverter.GetBytes, the are placed in the order that i would expect to get them (i.e., the leftmost 16 bits are the ones i want)
@sllev - I understand bitwise stuff quite well. I can read and understand what dtb is doing, i just couldn't come up with it on my own. Previously when i had done this kind of thing, we had swapped bytes, so i think that is a main ingredient of what i am missing here.
|
1

unit - 32bits Basically you should set 16 most significant bits to zero, so use bitwise AND operator:

uint newValue = 0x0000FFFF & uintValue;
answered Aug 17, 2011 at 20:33

2 Comments

I updated my problem statement. The data is given to me as a uint, but it is really 4 bytes. Basically, i have a long stream of data that is passed to me as uints, and i need to grab chunks of data out of that stream (8 bits here, 24 there, etc). The result that i listed above is the correct result based on my input data, i'm just trying to get the bits right.
Bitwise OR | does not set the 16 most significant bits to zero. 0x0000FFFF | 0x2C954B results in 0x2CFFFF. Do you mean bitwise AND &?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.