Waldir-PRG
asked on
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#?
"<<" 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);
}
for (i = 0; i < TAMANHO_LL; i++)
{
int shift = (TAMANHO_LL - 1 - i) * 8;
tamBloco += (logicalLength[i] & 0x000000FF) << shift;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes because C# also supports Left and Right Shift Operators...
Except >>> isn't a standard shift, hence the need to cast.
carl_tawn, I think your code is C++ not C#. Here is the equivalent C# code:
Or, if you define tamBuffer as either uint or ulong (unsigned integer types):
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
for(i2= 0; i2 < TAMANHO_LL; i2++)
{
offset = (ll.Length - 1 - i2) * 8;
ll[i] = (byte) ((((ulong)tamBuffer) >> offset) & 0xFF);
}
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);
}
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..
ASKER
Perfect. I wasn't using the cast long.
Thanks a lot.
Thanks a lot.
ASKER
Perfect. I wasn't using the cast "ulong".
Thanks a lot.
Thanks a lot.
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.
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.