Reputation: 1497
I'm trying to print scalar as it would look like if it was int32. That is, if I have 2532063508
, if I write it in 4 bytes and read as int32, I would read -1762903788
.
using int32
function in Matlab didn't work, because the way it works is, values outside the range [-2^31,2^31-1]
map to the nearest endpoint.
So I tried to use typecast
:
typecast(uint32(2532063508), 'int32')
works perfectly, but if I write e.g. -1 there, uint32() returns 0 so it fails.
P.S. I want it to work for signed integers as input as well, that is, for -1 it should return -1
any suggestions?
Upvotes: 2
Views: 74
Reputation: 15837
You can do calculations in int64
then convert to uint32
:
f = @(x)typecast(uint32(mod(int64(x),int64(2)^32)),'int32');
Or
function y = f(x)
y = typecast(uint32(mod(int64(x),int64(2)^32)),'int32');
end
So f([-1, 2532063508])
returns [-1, -1762903788]
.
Upvotes: 2