在matlab中自定义12位无符号整数,包裹溢出

时间:2016-06-13 16:35:16

标签: matlab

我正在使用代码,这要求我在MATLAB中有12位无符号整数,这也允许换行溢出。

所以基本上我想在matlab中做这样的事情:

uint12(4095) + uint12(1) // = 0
uint12(0) - uint12(1) // = 4095

我将如何实现这一目标?

注意:实际上我只需要整数数学,我不关心节省空间。我之所以想要这样,是因为所有代码都已经结构化了。

注2:定点设计师可用。

1 个答案:

答案 0 :(得分:0)

使用以下代码初始化您的值:

nBits = 12; % whatever you want
value = 53; % whatever you want
A = fi(value, 0, nBits, 0, ...
       'OverflowAction', 'Wrap', ...
       'SumMode', 'KeepLSB', ...
       'SumWordLength', nBits);

更新:行为示例

isSigned = 0; % Value is Unsigned
floatingPoints = 0; % Value is Integer
nBits = 10; % whatever you want
valueA = 1023; % This is Max value = 2^nBins - 1
A = fi(valueA, isSigned, nBits, floatingPoints, ...
       'OverflowAction', 'Wrap', ...
       'SumMode', 'KeepLSB', ...
       'SumWordLength', nBits);
valueB = 0;  % This is Min value
B = fi(valueB, isSigned, nBits, floatingPoints, ...
       'OverflowAction', 'Wrap', ...
       'SumMode', 'KeepLSB', ...
       'SumWordLength', nBits);

%% Testing
R = A + 1 % Displays R = 0    , and all it's properties => 0 is Min Value
R = B - 1 % Displays R = 1023 , and all it's properties => 1023 = 2^nBits - 1 => Max Value