Bit Flip-Bit Negation

35 ビュー (過去 30 日間)
Sal
Sal 2011 年 8 月 29 日
Hi, There is a way to do a single or multiple bit flip. For bit flip i mean the negation of only that bit in a decimal number. for example: bitflip(8,2) = 10 this should mean a negation on second bit of 8. Thank You

採用された回答

Walter Roberson
Walter Roberson 2011 年 8 月 29 日
bitflip = @(val, n) bitxor(val, 2.^(n-1));

その他の回答 (1 件)

Paulo Silva
Paulo Silva 2011 年 8 月 29 日
d=8; %decimal value
p=2; %position of the bit to flip
b=dec2bin(d); %convert to binary
%b(p)=num2str(~str2num(b(p))); %flip the bit (wrong place)
b(numel(b)+1-p)=num2str(~str2num(b(numel(b)+1-p))); %flip the bit
dbf=bin2dec(b); %convert to decimal and here you have your result

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by