3

I want to store -1 in smallest memory such as in {8||16||32} bits instead of 256 bits.

int i = -1;

When I return var, it returns -1L instead of -1.

I have assigned -1 into a int8 variable. As I know int8 is signed integer.

int8 i = -1;

But when I return output, it returns: '115792089237316195423570985008687907853269984665640564039457584007913129639679L' instead of -1.

[Q] What should I do to obtain -1?

Please note that I have experimented this problem in populus:

As contract I have use @RichardHorrocks's contract: NumTest on the answer.

test.py:

def test_receipt(web3, accounts, chain, unmigrated_chain):
    my_contract       = unmigrated_chain.get_contract('NumTest');

    set_txn_hash     = my_contract.transact().NumTest();
    contract_address = unmigrated_chain.wait.for_receipt(set_txn_hash)

    output           = my_contract.call().number();
    print(output);

Thank you for your valuable time and help.

1

This code returns -1 for me (for both int8 and int):

pragma solidity ^0.4.6;

contract NumTest {

    int8 i;

    function NumTest() {
        i = -1;
    }

    function number() constant returns (int8 num) {
        return i;    
    }
}

As has been mentioned in previous threads, due to unpacking costs, the int8 case (278 gas) is actually more expensive than the default int/int256 case (224 gas).

For the above code, and for completeness (and because I was interested):

integer bits | gas cost
-------------+---------
     8       |   278
    16       |   278
    32       |   278
    64       |   278
   128       |   278
   248       |   278
   256       |   224

So unpacking costs are the same for anything below the optimal size.

  • I am very sorry related to I did not mention that I experienced this problem in populus and since it was not working in populus, I assumed it will not work in real blockchain environment. I guess the problem may related to python or populus I guess. I have updated my question with my test case.@RichardHorrocks – alper Feb 21 '17 at 12:31
  • Ah, okay - thanks for adding the extra details. I'll leave my answer in case it's of use to anyone else, though I realise it doesn't answer your specific question anymore! – Richard Horrocks Feb 21 '17 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.