Questions tagged [integers]

The tag has no usage guidance.

29 questions
Filter by
Sorted by
Tagged with
16 views

Why does negation flip all high bits of a number expect for the least significant bit?

In his blog post on 512 bit division, Remco Bloemen makes the following assertion: ... negation flips all high bits of a number up to but not including the least significant one Why is that?
71 views

Complicated ERC20 Arithmetic

I'm trying to compose a formula in Solidity that has a combination of unsigned integers, signed and exponents, however having a hard time figuring out how to do so. The formula would look something ...
• 101
444 views

How absolute value work in solidity? [duplicate]

For some reason, I will get two values which are uint256. How do I calculate uint256 then get the absolute value? uint256 a = 5; uint256 b = 9; return a-b; // will be return 4 return b-a; // will be ...
• 177
1 vote
614 views

How to convert default two's complement representation to sign magnitude form?

According to this answer the EVM by default uses two's complement notation for handling signed integers. However for my application I would like to work with the sign-magnitude representation where ...
• 1,663
1k views

How do you handle the hex prefix without the value (only "0x")?

First, I'm caling function foo() (from another contract) that returns me a uint in the form of bytes. Then, I'm using BytesLib.toUint(foo(), 0) to retrieve the uint. When the value is supposed to be 1,...
488 views

What is the fastest way to check the sign of an int256 in Solidity?

Given an int256 number n, I want to get the sign of n: If n is positive, get 1 If n is negative, get -1 If n is 0, get 0 I know that I can do: function foo(int256 x) external { if (x > 0) { ...
809 views

Validation of integers on solidity

I have this function and require the _price variable to be positive, But when I pass a negative int, it accepts the integer! How can I validate the input? The Contract : function createProduct(string ...
1 vote
2k views

How to get absolute value in solidity?

In JavaScript, there is "Math.abs": Is there a similar function in solidity itself or some other popular library such as SafeMath?
1 vote
479 views

How to verify the sha256 result of integer 5 in the Zokrates tutorial?

The Zokrates tutorial here starts with calculating the SHA256 hash of (the number) 5. The hex representation of the result is shown at the bottom here: However, I could not reproduce this on my ...
833 views

Synthetic uint512 out of 2 uint256

If I were trying to make a synthetic unsigned integer_512, but the largest supported integer in Ethereum is u256, how would I combine 2 u256 to make a single u512 in solidity? If I were also looking ...
• 213
57 views

what is the different between this two? [closed]

today I have two type One is like this pragma solidity ^0.5.0; contract Test{ function TestVar() view public returns(uint8){ uint8 a; for(uint8 b=0;b<20; b++){ a=b; } ...
103 views

Why doesn't Solidity throw an error for overflows? [duplicate]

It seems like there are going to be a lot of people that forget to check for integer overflows in Solidity. I am wondering if there is a reason why Solidity doesn't throw errors for integer overflows ...
• 149
503 views

Increment a uint variable results in unexpected value

I have the function resolveProject() in my contract, which iterates over a list of Task, let's say 2, both with status 3. function resolveProject() { // ... uint closedTask; uint ...
• 13
442 views

Solidity: uint parameter not being set properly

Testing my contract locally with truffle. I have a function with the signature: function purchase(address buyer, address seller, bytes16 bookId, uint tokens) public onlyOwner returns(bool success) ...
• 141
1 vote
933 views

Truffle test: inputting an uint256 outputs a different result

To simplify my question, let's say I have a Test.sol with a function that simply output the input: function testFunc(uint256 value) public pure returns (uint256) { return value; } Now I want to ...
• 181
2k views

How to store a Big Number?

I need to determine an expiry date for a contract a for this I use the foloowing code as the constructor of the contract : constructor(address _recipient, uint256 duration) public ...
• 2,670
1 vote
969 views

negative numbers passed to view functions are treated differently to negative numbers passed to transaction functions

Using solidity 0.4.18 I have the following contract: contract MyContact { mapping(address => uint) private balances; modifier positiveNumber(uint number) { require(number > 0);...
• 879
105k views

What is uint256?

With regard to Solidity, What is UINT256? From the token example at https://ethereum.org/token : /* This creates an array with all balances */ mapping (address => uint256) public balanceOf; ...
17k views

Max/min values of standard data types

Where can I find the maximum and minimum values that various types can store? And an epsilon value for floats? I'm looking for something like C's limits.h, but for Solidity / EVM.
• 3,107
370 views

Invalid opcode during division

I have seen many posts around the division operation in Solidity e.g. Cant' do any integer division. Firstly, the error I am facing is different. Secondly, I am ensuring that, all numbers in division ...
1 vote
175 views

Can it be shown that `num > 0 && num < denom` is always the same as 0% < num / denom < 100%?

In Solidity, I want to check that the numerator and denominator passed to my function evaluate to 0% < num / denom < 100%. I tried the most direct approach: num / denom > 0 && num / ...
• 115
22k views

Can't do any integer division

I'm building a new smart contract but really can't figure out how to perform integer division. I know that fixed point numbers haven't been implemented yet but it should be possible to perform 100/3 = ...
• 273
806 views

How to store negative values inside {8||16||32} bits in populus?

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 ...
• 8,395
5k views

Type casting msg.value to uint248

I am new to solidity programming and sorry if my question is very basic. I am wondering if we can cast msg.value to uint248 Example below ; uint value; value = (uint248) (msg.value);
• 241
1 vote
636 views

Not able to read data from smart contracts

I am doing PoC on Ethereum using Azure Ethereum network and Web3. I was working from last 1 month and I have created one smart contract for student management and that was working like fab. But for ...
• 29
106 views

VM jump when accessing statically set variables

Hey it seems I get a jump error when I try to access any variable not set through a parameter in the constructor. amountRaised is throwing a jump error below as I try to statically set the value. ...
• 1,805
1k views

Difference between the assembly code generated for uint and uint32?

I wrote a simple contract with a single state variable with type uint and the generated assembly code is quite straight forward. But when I changed the type to uint32 the generated assembly becomes ...
• 113
262 views

int256 range on geth command line

When I do the following on the geth command line: 20000000000000000000-19999999999999999989 it returns 0 I'm guessing this is because of a 64 bit limit on ints. Is there a way to get it to return ...
• 8,548