extract one of the bits in fi(), flip it, and reassemble it to produce new bin

10 ビュー (過去 30 日間)
scc28x
scc28x 2021 年 4 月 16 日
回答済み: Andy Bartlett 2021 年 4 月 19 日
Hi, I am trying to do the following cleanly in fixed point designer:
Take a binary: '0010'
Flip the 2nd bit to produce: '0000'
Is there a clean way to do this with some opeartion within fi(), perhaps just 1 or 2 lines of code?
I am a new user at fixed-point designer.
Thank you very much.

採用された回答

Hrishikesh Borate
Hrishikesh Borate 2021 年 4 月 19 日
Hi,
It’s my understanding that you are trying to flip the bit at 2nd position.
Following is the code for the same :-
input = '0010';
bitflip = @(val, n) bitxor(val, 2.^(n-1));
output = dec2bin(bitflip(bin2dec(input),2));
For more information, refer to the following answer and dec2bin.

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2021 年 4 月 19 日
input = '1010';
input(3) = 'a' - input(3)
input = '1000'
... It's magic ;-)
flipped = (1 - (x - '0')) + '0'
flipped = 1 + '0' + '0' - x
flipped = 1 + 48 + 48 - x
flipped = 97 - x
flipped = 'a' - x

Andy Bartlett
Andy Bartlett 2021 年 4 月 19 日
Please see my answer in your similar question.
That answer provides a one-liner that works in most cases.
It also provides a more general solution using a helper function.

カテゴリ

Help Center および File ExchangeFixed-Point Designer についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by