Bitwise Or and bitwise shifting.....

  • Thread starter Thread starter James Dean
  • Start date Start date
J

James Dean

I am recoding a project in C#.....i just wanted to know if these are
equivalent and give the same result....

old C++ code
for ( long loop = 0; loop < ( longWidth_bytes - 1); loop++)
{
*lpbLine = ( *lpbLine >> charShift) |
( *( lpbLine - 1) << charShift_inv) ;
lpbLine-- ;
}

for ( long loop = 0; loop <= (long_WidthValue - 1); loop++)
{
LineBytes[pointerPos] =
Convert.ToByte(LineBytes[pointerPos] >> charShift);
byte prevbyte =
Convert.ToByte(LineBytes[prevPos] << charShift_inv);
LineBytes[pointerPos] =
Convert.ToByte(LineBytes[pointerPos] | prevbyte);
pointerPos--;
prevPos--;
}

I have to shift the data in the amount of this charshift so i have to do
it for every byte. As you see i had use this "Convert To Byte" function
alot to make it work. I don't think this is right. Could anybody suggest
a better way?


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Hi James,

If you need to do extensive bit manipulation, you might want to take a look at BitArray.
 
Morten,
Well i thought about using BitArray class. Like converting the bytes to
bits shifting them then copying them back to a byte array again.....but
would this cause too many performance issues?.....


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
I could not say as I haven't compared or seen any comparison

A problem with bitarrays is that you can only hold data up to Int.MaxValue. If this isn't an issue you can try comparing this with your own code.

//byte[]b = new byte[]{124,123,234,0,2};

//BitArray ba = new BitArray(b);
//BitArray ba2 = new BitArray(ba.Length, false);

//int charshift = -1;

for(int i = 0; i < ba.Length; i++)
{
if(charshift + i > 0 && charshift + i < ba.Length)
ba2[charshift + i] = ba;
}

//byte[] b2 = new byte[b.Length];
//ba2.CopyTo(b2, 0);
 
unsafe
{
fixed (byte* pData=data)
{
byte* lpbLine = pData+data.Length-1;
for ( long loop = 0; loop < ( data.Length - 1); loop++)
{
*lpbLine = (byte) (( *lpbLine >> charShift) | ( *( lpbLine - 1) <<
charShift_inv));
lpbLine-- ;
}
}
}

Is a 1:1 translation of your C code. I guess you need the (byte) cast
because '<<' might have result > 255, and the compiler wants you to be aware
of the fact that the cast may throw away data.
The "safe" equivalent would be:

for ( int index = data.Length-1; index > 0; index--)
data[index] = (byte) (data[index] >> charShift | data[index-1] <<
charShift_inv);

I'm not sure if the JIT is smart enough to remove those indexing
instructions;

If you really need performance I'd suggest an unsafe version that operates
on int's or even long's instead of bytes.

Niki
 
James Dean said:
I am recoding a project in C#.....i just wanted to know if these are
equivalent and give the same result....

old C++ code
for ( long loop = 0; loop < ( longWidth_bytes - 1); loop++)
{
*lpbLine = ( *lpbLine >> charShift) |
( *( lpbLine - 1) << charShift_inv) ;
lpbLine-- ;
}

for ( long loop = 0; loop <= (long_WidthValue - 1); loop++)
{
LineBytes[pointerPos] =
Convert.ToByte(LineBytes[pointerPos] >> charShift);
byte prevbyte =
Convert.ToByte(LineBytes[prevPos] << charShift_inv);
LineBytes[pointerPos] =
Convert.ToByte(LineBytes[pointerPos] | prevbyte);
pointerPos--;
prevPos--;
}

I have to shift the data in the amount of this charshift so i have to do
it for every byte. As you see i had use this "Convert To Byte" function
alot to make it work. I don't think this is right. Could anybody suggest
a better way?

Just cast to byte. The reason you need to cast is that result of any
shift operation is either an int or a long, depending on the original
type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top