How can I change the LSBs of consecutive elements of an array?
5 次查看(过去 30 天)
显示 更早的评论
I have a row matrix a = [23, 255, 67, 89, 45, 90, 34, 12] and a number, n = 3 (of 8 bits) which I want to hide in the LSBs of every element. 3 is '00000011', so LSB of 23 should change to 0 (if not already 0), LSB of 255 should change to 0 and so on till 12. n is a variable from 2-8 of 8 bits.
I tried the following code:
n=3;
a=[23, 255, 67, 89, 45, 90, 34, 12];
str=dec2bin(n,8);
for i=1:8
b(1,i)=bitset(a(1,i),8,str(i));
end
I get the following error: ASSUMEDTYPE must be an integer type name.
How do I convert the string 'str' to be a numeric array of the 1s and 0s?
0 个评论
回答(2 个)
Joseph Cheng
2017-6-2
编辑:Joseph Cheng
2017-6-2
you could use bitand()
a = [23, 255, 67, 89, 45, 90, 34, 12]';
n=3;
b = bitand(a ,255-n,'uint8')
disp(dec2bin(a))
disp('zeroed out last 2 bits')
disp(dec2bin(b))
i know i didn't implement your selection of n correctly as i wasn't able to follow what you were describing with lsbs of 3 and the 23 example, however the bit wise operators should work whatever you're trying to describe
Guillaume
2017-6-2
The third argument of bitset must either be the number 0 or 1 or a char array representing a valid type (such as 'uint8'). You're giving it the char '0' or '1'. Change your line to:
b(1, i) = bitset(a(1, i), 8, str(i) - '0'); %or str(i) == '1', whichever you prefer
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!