convertion from java to c#

Waldir-PRG
Waldir-PRG used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Systems and Integration Developer
Commented:
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.
Yes because C# also supports Left and Right Shift Operators...
Carl TawnSenior Systems and Integration Developer

Commented:
Except >>> isn't a standard shift, hence the need to cast.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Top Expert 2011

Commented:
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
triple shift isn`t supported so you may shift twice then once again to shift thrice..

Author

Commented:
Perfect. I wasn't using the cast long.
Thanks a lot.

Author

Commented:
Perfect. I wasn't using the cast "ulong".
Thanks a lot.
Top Expert 2011

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial