change fixed point number signedness
조회 수: 6 (최근 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!