# what happens if I do using SafeMath for uint256 but then divide by uint8?

example

``````using SafeMath for uint256;

uint8 number8 = 10;
uint256 number256 = 1000;

function something() {
return number256.div(number8);
}

``````

is it still using safemath even if my number8 is uint8?

• Yes, it is safe. `number8` is expanded to `uint256` of the same numeric value. Commented Oct 5, 2020 at 6:58

It won't compile as SafeMath is expecting a `uint256` (update: actually it does compile and auto-casts it into uint256, but I would recommend being explicit anyways). In your case a simple cast will fix it:

``````number256.div(uint256(number8))
``````

For the other way around, meaning you want the final result to be a `uint8`, you can use the SafeCast functionality to go around this:

``````SafeCast.toUint8(number256.div(uint256(number8)))
``````

This will revert if the final number is bigger than 255 (2^8 -1), the MAX_UINT8.

Update: Actually it does still compile, but be very careful. SafeMath is supposed to be used only with uint256, so trying to use anything else won't give the expected results. For example:

``````uint8(2).mul(uint8(128))
``````

will not revert, but rather give you 256 as a `uint256`.

• `It won't compile` - have you tried it? From my experience, the expression `number256.div(number8)` very well compiles. The expression `number8.div(number256)` won't compile, but that's not what the question is all about. Commented Oct 5, 2020 at 6:57
• @goodvibration did you read my comment in the brackets and at the bottom? Commented Oct 5, 2020 at 7:07
• I just read the very first sentence. Commented Oct 5, 2020 at 7:09