Hex representation of negative integers in Vala

Hi. I am not sure if this is a bug or a feature but I thought it would be a good idea ask here because Vala handles the negative sign in a way that seems to be different from C.

This statement in C works for me:

   int32_t x = 0xffffcccc;
   assert (x == 0xffffcccc);

but the same thing in Vala causes me some trouble:


	// fiddle with bits, the details are not important but this function will return 0xffffcccc (in this example).
	int32 to_fixed (double d) { 
		int32 val = (int32) Math.floor (d);
		int32 mant = (int32) Math.floor (0x10000 * (d - val));
		val = (val << 16) | mant;
		return val;
	}
	
	void test () {
		int32 f;

		f = 0xffffcccc; // does not compile because "Assignment: Cannot convert from `int64' to `int32'"
		assert (f == 0xffffcccc);
				
		f = to_fixed (-0.2); // this will set f to 0xffffcccc (it does compile)
		
		// this will print "0xffffcccc == 0xffffcccc ?"
		stdout.printf ("0xffffcccc == 0x%x ?\n", f); 
		
		// will fail, f is not 0xffffcccc 
		assert (f == 0xffffcccc); 	
	}

Is there a way around this?

I guess the expression 0xffffcccc returns a 64 bit integer. This is why you cannot assign it to a int32 variable. And I think the assertion fails, because f will be casted to int64. Can you maybe cast 0xffffcccc explicitly to int32?

Thank you for taking the time to respond this. Yes, it looks like explicitly casting to int32 works, maybe a warning for the assert case is enough to make sure that this doesn’t happen to other developers.

assert (f == 0xffffcccc);

Cheers

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.