Bitwise addition if bit is 1.but not gettin answer.
1 次查看(过去 30 天)
显示 更早的评论
I am taking one number and converting it into binary and checking each bit whether it is 1 and if it is 1 then is added to next bit which is 1 and result is stored in c.
clc;
q=347;
q=dec2bin(q)
n=size(q,2)
c='0'
for i=1:n
if q(i)==1
c=c+q(i)
end
end
0 个评论
采纳的回答
Azzi Abdelmalek
2013-12-27
编辑:Azzi Abdelmalek
2013-12-27
Maybe you want
q=347;
q=dec2bin(q)
c=sum(q=='1')
%or
c=numel(strrep(q,'0',''))
0 个评论
更多回答(1 个)
Amit
2013-12-27
The question is unclear. Can you elaborate?
One issue I see is that q(i) == '1' and not q(i) == 1. As q is a string and not an integer.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!