I am trying to encode 3 signed integers into 3 - 32 bits slots using bit shifting, which works fine but not with negative numbers. This could be a simple problem in C++ with the negative conversion but I never work on C++ or know any good resources to follow.
Could anyone please shed light of how to deal with a negative number in this situation?
Really appreciate it.
function encodeNumbers(int256 a, int256 b, int256 c) public pure returns(uint256 encoded) {
encoded |= uint(a) << 64;
encoded |= uint(b) << 32;
encoded |= uint(c);
return encoded;
}
function decodeNumber(uint256 encoded) public pure returns (int256 a, int256 b, int256 c) {
a = int(encoded) >> 64;
b = (int(encoded) << 192) >> 224;
c = (int(encoded) << 224) >> 224;
}