473,378 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Bitwise Or and bitwise shifting.....

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!
Nov 16 '05 #1
5 2170
Hi James,

If you need to do extensive bit manipulation, you might want to take a look at BitArray.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
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!
Nov 16 '05 #3
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[i];
}

//byte[] b2 = new byte[b.Length];
//ba2.CopyTo(b2, 0);
--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4
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" <m_*******@yahoo.com> wrote in
news:%2****************@TK2MSFTNGP11.phx.gbl...
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!

Nov 16 '05 #5
James Dean <m_*******@yahoo.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alef T Veld | last post by:
Hello, i want to make a scanner for system management purposes. what is the best way to do this. should i just do it in decimal or would working with bits be faster/better? and how would one...
13
by: Manish_Ganvir | last post by:
Please do not use pointer arithmetic or for loops Solution
2
by: joshc | last post by:
I have a question about the following: 4. The result of E1 << E2 is E1 left-shifted E2 bit positions: vacated bits are filled with zeros. ... If E1 has a signed type and a nonnegative value, and...
6
by: gilad | last post by:
I am trying to return 8 bytes (64 bits) as a 'long' from a file my program is reading. The logic I am using is below: return (long)( ((inBytes & 255) << 56) | ((inBytes & 255) << 48) |...
2
by: john conwell | last post by:
I have 3 5-digit numbers that i'd like to combine into one 15 digit number, and later rip back into the original 3 5-digit numbers. I know i can do this easily with some string manipulations, but...
3
by: shdwsclan | last post by:
I am native to various languages but bitwise operators just kill me. I see how much I take object oriented languages for granted. I like all the other c derivitives but ANSI C is making me loose my...
13
by: HARDCORECODER | last post by:
ok here is the question. I want to exract the first 4 bits in a int so let say int b = somenumber; int result= 0; result = b << 4; if I got this right result should contain the 4 bits that...
5
by: Rahul | last post by:
Hi Everyone, I have a program unit which does >and << of an integer which is of 4 bytes length. The logic of shifting and action based on the result, assumes that the system is big-endian. ...
6
by: Ramtin Kazemi | last post by:
Hi How can i perform bitwise rotation in C#?
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.