Finding a bit value ?

Hi. I have a value like a = 10101 this would change after how my program is run, but i will always use 0 or 1. so i consider this binary. now i would like to get the status of bit 5 i.e the first number (closest to the equal sign).
note!: the intention is to have the binary row decied which buttons are displayed in a gui.
this is what i have come up with
%code
for i = 0:4
count = i+1
if (bitget(main_sel2, count)==1)
set(handles.(sprintf('but%d', count))(:),'Visible','on');
else
set(handles.(sprintf('but%d', count))(:),'Visible','off');
end
end

1 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 5 月 22 日
What is your question?

サインインしてコメントする。

回答 (1 件)

Matt J
Matt J 2013 年 5 月 22 日
編集済み: Matt J 2013 年 5 月 22 日

0 投票

One way,
>> bits=fliplr(num2str(a)-'0'); bits(5)
ans =
1

4 件のコメント

Matt J
Matt J 2013 年 5 月 25 日
Aleksander commented:
No i do not think fliplr vil help her.
-to explaine: i have A gui with 5 buttons (more will be added latter) now i wish to store the string "10101" in text file, so that the user can select which button to use. Thea means that each segment in the 5 segment number coresponds to button, the trick is how to read each segment ??
As above a thought using bitget would do, but i might missunderstand the usage. Would need do to
%code
a = bitget(main_sel2, 16).
?
Matt J
Matt J 2013 年 5 月 25 日
編集済み: Matt J 2013 年 5 月 25 日
Thea means that each segment in the 5 segment number coresponds to button, the trick is how to read each segment ??
Since you have "10101" in string form, you can index each digit in the string directly and convert it to numeric form, which is what I showed you in the solution I proposed. BITGET is not applicable here.
Aleksander
Aleksander 2013 年 5 月 25 日
Thanks, i tested it now and looks to work as i want :) Sine i am a nub can you try to enlighten me how fliplr actualy dose This ? Thanks.
Matt J
Matt J 2013 年 5 月 25 日
FLIPLR isn't doing anything significant. You posted that you want the indexing order of the bits to run from right to left (bit #5 is the leftmost bit). If you omit FLIPLR, you will be reading from left to right instead.

サインインしてコメントする。

質問済み:

2013 年 5 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by