change fixed point number signedness
14 次查看(过去 30 天)
显示 更早的评论
Hi, I have a fixed point number which is unsigned. How do I cast it to a signed fi by taking the bits literally:
eg.
two cases:
a=fi(3,0,3,0); %case1: a='011'
a=fi(4,0,3,0); %case2: a='100'
Now define a new b=fi(....) based on 'a' such that
case1: b.bin will be '011' and is signed
case2: b.bin will be '100' and is signed
Thanks.
0 个评论
回答(2 个)
stozaki
2021-4-19
Hi scc28x,
>> b1 = fi(3,1,3,0)
b1 =
3
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 3
FractionLength: 0
>> bin1 = b1.bin
bin1 =
'011'
>> b2 = fi(4,1,4,0)
b2 =
4
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 4
FractionLength: 0
>> bin2 = b2.bin
bin2 =
'0100'
b2 is carried by sign extension.
Regards,
stozaki
Andy Bartlett
2021-4-19
reinterpretcast is the solution
u = fi(129,0,8,0)
ntu = numerictype(u)
nty = numerictype(ntu,'SignednessBool',true)
y = reinterpretcast(u,nty)
[u.bin;y.bin]
which outputs
u =
129
numerictype(0,8,0)
ntu =
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 8
FractionLength: 0
nty =
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 0
y =
-127
numerictype(1,8,0)
ans =
2×8 char array
'10000001'
'10000001'
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Create Fixed-Point Objects in MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!