convertion from java to c#

I need to convert a code from java to c#, but I'm having problem with some operators.

"<<" and ">>>"

How can I write the last line of each command for() to c#?


for(i2= 0; i2 < TAMANHO_LL; i2++) 
{
   offset = (ll.Length - 1 - i2) * 8;
   ll[i] = (byte) ((tamBuffer >>> offset) & 0xFF); 
}

Open in new window

for (i = 0; i < TAMANHO_LL; i++) 
{
   int shift = (TAMANHO_LL - 1 - i) * 8; 
   tamBloco += (logicalLength[i] & 0x000000FF) << shift; 
}

Open in new window

Waldir-PRGAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
For the first one try:
for(i2= 0; i2 < TAMANHO_LL; i2++) 
{
   offset = (ll.Length - 1 - i2) * 8;
   ll[i] = (byte) (((unsigned long)tamBuffer >> offset) & 0xFF); 
}

Open in new window

The second one should be ok as it is.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
starlite551Commented:
Yes because C# also supports Left and Right Shift Operators...
0
Carl TawnSystems and Integration DeveloperCommented:
Except >>> isn't a standard shift, hence the need to cast.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

wdosanjosCommented:
carl_tawn, I think your code is C++ not C#.  Here is the equivalent C# code:

for(i2= 0; i2 < TAMANHO_LL; i2++) 
{
   offset = (ll.Length - 1 - i2) * 8;
   ll[i] = (byte) ((((ulong)tamBuffer) >> offset) & 0xFF); 
}

Open in new window


Or, if you define tamBuffer as either uint or ulong (unsigned integer types):

for(i2= 0; i2 < TAMANHO_LL; i2++) 
{
   offset = (ll.Length - 1 - i2) * 8;
   ll[i] = (byte) ((tamBuffer >> offset) & 0xFF); 
}

Open in new window


The second one is good, as carl_tawn already indicated.

More about C# right swift operator:
http://msdn.microsoft.com/en-us/library/xt18et0d%28v=VS.71%29.aspx
0
starlite551Commented:
triple shift isn`t supported so you may shift twice then once again to shift thrice..
0
Waldir-PRGAuthor Commented:
Perfect. I wasn't using the cast long.
Thanks a lot.
0
Waldir-PRGAuthor Commented:
Perfect. I wasn't using the cast "ulong".
Thanks a lot.
0
wdosanjosCommented:
starlite551, the >>> in Java is an unsigned right shift operator, ie shifts a zero into the leftmost position.  The >> is a signed right shift operator, so the left most position depends on the sign.  This distinction is necessary because Java does not have unsigned integer data types.

Since C# has unsigned integer data types, the >> operator cares about sign on int and long types, but it works as a Java >>> for the unsigned types (uint and ulong).

. Java Bitwise and Bit Shift Operators
  http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

. C# >> Operator
  http://msdn.microsoft.com/en-us/library/xt18et0d%28v=VS.71%29.aspx

I hope this helps.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.