change fixed point number signedness
14 views (last 30 days)
Show older comments
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 Comments
Answers (2)
stozaki
on 19 Apr 2021
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
on 19 Apr 2021
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 Comments
See Also
Categories
Find more on Create Fixed-Point Objects in MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!