フィルターのクリア

Disabling integer overflow capping for uint8 datatypes

14 ビュー (過去 30 日間)
JMB
JMB 2024 年 6 月 20 日
編集済み: Voss 2024 年 6 月 20 日
Hello!
I am currently in the middle of optimizing some high-performance code, and have run into a weird situation where I need integer overflow to occur on some uint8 variables. Unfortunately, it seems like MATLAB features datatype capping, meaning that when I perform the operation
uint8(100) * uint8(30)
I get a value of 255, where I would expect something like 184.
The same occurs when I perform addition:
uint8(100) + uint8(200)
Here, I also expect to get 44, but instead get 255.
Is there a way to turn this capping off, so that I can have the overflow occur? I understand that this could potentially be a "dangerous" operation to perform, but I really would benefit from having a way to test some code with integer overflow in MATLAB before I do any form of compilation or other code optimization work.

回答 (1 件)

Voss
Voss 2024 年 6 月 20 日
編集済み: Voss 2024 年 6 月 20 日
I don't think you can disable integer overflow capping, but you could of course perform the operations on corresponding floating-point variables and then take the results mod 256.
x = uint8(100);
y = uint8(30);
z = uint8(200);
% original integer arithmetic
x * y
ans = uint8 255
x + z
ans = uint8 255
% with capping "disabled"
uint8( mod( double(x) * double(y) , 256) )
ans = uint8 184
uint8( mod( double(x) + double(z) , 256) )
ans = uint8 44

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by