Error in converting String column to binary column [duplicate] - scala

I have ported Java code to C#.
Could you please explain why I have compile-time error in the follow line (I use VS 2008):
private long l = 0xffffffffffffffffL; // 16 'f' got here
Cannot convert source type ulong to target type long
I need the same value here as for origin Java code.

Java doesn't mind if a constant overflows in this particular situation - the value you've given is actually -1.
The simplest way of achieving the same effect in C# is:
private long l = -1;
If you want to retain the 16 fs you could use:
private long l = unchecked((long) 0xffffffffffffffffUL);
If you actually want the maximum value for a signed long, you should use:
// Java
private long l = Long.MAX_VALUE;
// C#
private long l = long.MaxValue;

Assuming you aren't worried about negative values, you could try using an unsigned long:
private ulong l = 0xffffffffffffffffL;
In Java the actual value of l would be -1, because it would overflow the 2^63 - 1 maximum value, so you could just replace your constant with -1.

0xffffffffffffffff is larger than a signed long can represent.
You can insert a cast:
private long l = unchecked( (long)0xffffffffffffffffL);
Since C# uses two's complement, 0xffffffffffffffff represents -1:
private long l = -1;
Or declare the variable as unsigned, which is probably the cleanest choice if you want to represent bit patterns:
private ulong l = 0xffffffffffffffffL;
private ulong l = ulong.MaxValue;
The maximal value of a singed long is:
private long l = 0x7fffffffffffffffL;
But that's better written as long.MaxValue.

You could do this:
private long l = long.MaxValue;
... but as mdm pointed out, you probably actually want a ulong.
private ulong l = ulong.MaxValue;

Related

Chisel UInt negative value error

I have recently started work in scala, and am required to create an implementation of MD5. It is my understanding that MD5 requires unsigned types, which scala does not come with. As I will soon begin Chisel, which does have unsigned types, I decided to implement its library. Everything appears good so far, except when doing the below bitwise operations, my F value becomes -271733879, which causes an error "Caused by: java.lang.IllegalArgumentException: requirement failed: UInt literal -271733879 is negative" as UInts can't be negative.
if(i<16){
F = ((B & C) | ((~B) & D))
g = i
}
There is more to the error message, but it is just the trace list of different libraries and classes that had an error because of this error, and thus I did not post it because I didn't think it was important. If it was, I can edit this and post it all.
My B, C, and D values are equal to the lower case equivalents listed below, and it is the first time through the for loop so they have not yet updated.
var a0 : UInt = UInt(0x67452301)
var b0 : UInt = UInt(0xefcdab89)
var c0 : UInt = UInt(0x98badcfe)
var d0 : UInt = UInt(0x10325476)
Any Help would be greatly appreciated.
For the sake of my answer, I am using the Chisel 3 preferred 123.U style for specifying literals rather than the Chisel 2 UInt(123) style, but this answer works for either.
There are several ways you could do this:
Use Scala Long (put L at end of literal)
val myUInt = 0x98badcfeL.U
This obviously won't work for larger than 64-bit
Use Scala BigInt
val myUInt = BigInt("98badcfe", 16).U
Use Chisel's shorthand for constructing BigInts from Strings
val myUInt = "x98badcfe".U
hex = x | h, dec = d, oct = o, bin = b

why chisel UInt(32.W) can not take a unsigned number which bit[32] happens to be 1?

It is defined that UInt is the type of unsigned integer. But in such case it seems like the MSB is still a sign. e.g., the most relative QA is Chisel UInt negative value error which works out a workaround but no why. Could you enlight me about the 'why'?
The UInt seems to be defined in chisel3/chiselFrontend/src/main/scala/chisel3/core/Bits.scala but I cannot understand the details. Is the UInt is derived from Bits and Bits is derived from Int of scala?
The simple answer is that this is due to how Scala evaluates things.
Consider an example like
val x = 0xFFFFFFFF.U
This statement causes an error.
UInt literal are represented internally by BigInts, but the 0xFFFFFFFF is an specifying an Int value. 0xFFFFFFFF is equivalent to the Int value -1.
The -1 Int value is converted to BigInt -1 and -1.U is illegal because the .U literal creation method will not accept negative values.
Adding the L fixes this because 0xFFFFFFFL is a positive Long value.
The issue is that Scala only has signed integers, it does not have an unsigned integer type. From the REPL
scala> 0x9456789a
res1: Int = -1806272358
Thus, Chisel only sees the negative number. UInts obviously cannot be negative so Chisel reports an error.
You can always cast from an SInt to a UInt if you want the raw 2's complement representation of a negative number interpreted as a UInt. eg.
val a = -1.S(32.W).asUInt
assert(a === "xffffffff".U)

D language unsigned hash of string

I am a complete beginner with the D language.
How to get, as an uint unsigned 32 bits integer in the D language, some hash of a string...
I need a quick and dirty hash code (I don't care much about the "randomness" or the "lack of collision", I care slightly more about performance).
import std.digest.crc;
uint string_hash(string s) {
return crc320f(s);
}
is not good...
(using gdc-5 on Linux/x86-64 with phobos-2)
While Adams answer does exactly what you're looking for, you can also use a union to do the casting.
This is a pretty useful trick so may as well put it here:
/**
* Returns a crc32Of hash of a string
* Uses a union to store the ubyte[]
* And then simply reads that memory as a uint
*/
uint string_hash(string s){
import std.digest.crc;
union hashUnion{
ubyte[4] hashArray;
uint hashNumber;
}
hashUnion x;
x.hashArray = crc32Of(s); // stores the result of crc32Of into the array.
return x.hashNumber; // reads the exact same memory as the hashArray
// but reads it as a uint.
}
A really quick thing could just be this:
uint string_hash(string s) {
import std.digest.crc;
auto r = crc32Of(s);
return *(cast(uint*) r.ptr);
}
Since crc32Of returns a ubyte[4] instead of the uint you want, a conversion is necessary, but since ubyte[4] and uint are the same thing to the machine, we can just do a reinterpret cast with the pointer trick seen there to convert types for free at runtime.

Dart constant time string comparison

I'm implementing a github push hook listener in dart, and I've come across this document: https://developer.github.com/webhooks/securing/
where it's written:
Using a plain == operator is not advised. A method like secure_compare
performs a “constant time” string comparison, which renders it safe
from certain timing attacks against regular equality operators.
I have to compare 2 hashes for equality. Now I was wondering if there was a way to compare string in constant time in dart? (read: is there a string constant time compare function in dart?)
The default implementation is not constant time, but you can just create your own comparison function that compares every code unit in the String and does not short circuit:
bool secureCompare(String a, String b) {
if(a.codeUnits.length != b.codeUnits.length)
return false;
var r = 0;
for(int i = 0; i < a.codeUnits.length; i++) {
r |= a.codeUnitAt(i) ^ b.codeUnitAt(i);
}
return r == 0;
}
This function will perform a constant time String compare as long as the two input Strings are of the same length. Since you are comparing hashes this shouldn't be a problem, but for variable length Strings this method will still leak timing info because it immediately returns if the lengths are not equal.

get the abs value of long long integer

I try to use the codes below to get the abs value of a long long type integer;
long long v=abs(originalValue);
It works as I expected until the value of v exceeds 1073741824 (1G)
If v is 2147482648, abs(v) is -2147482648.
If v is 10737418240, abs(v) is -2147482648 also.
I do not understand what causes these happened.
Welcome any comment
Thanks
interdev
Use llabs() instead:
long long v = llabs(originalValue);
abs takes an int as an argument, for a long long use llabs