# How do I do unsigned comparsion between two vectors having 1s and 0s?

1 回表示 (過去 30 日間)
Nadatimuj 2022 年 3 月 29 日
コメント済み: Chunru 2022 年 3 月 29 日
x = '10000001000000000000000000000000'
y = '00000000000001010110001111000000'
Let's say x is the binary representation of a 32-bit unsigned number. y is also same.
I want to have unsigned comparison between both of them. That means x(1) and y(1) are the MSB and the comparsion should start from there till we reach LSB.
Thanks.

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

### 採用された回答

Chunru 2022 年 3 月 29 日
x = '10000001000000000000000000000000'
x = '10000001000000000000000000000000'
y = '00000000000001010110001111000000'
y = '00000000000001010110001111000000'
% Convert the binary to decimal
xd = bin2dec(x)
xd = 2.1643e+09
yd = bin2dec(y)
yd = 353216
% compare the decimal number
xd > yd
ans = logical
1
##### 2 件のコメントなしを表示なしを非表示
Nadatimuj 2022 年 3 月 29 日
Thanks. This works well for the 32-bit. Will it work for 64-bit as well?
Chunru 2022 年 3 月 29 日
It works forup to 53 binary bits (?).
If you have more bits to compare, you can use symbolic computation:
x = '0b100000010000000000000000000000001000000000000000000000000011';
y = '0b000000000000010101100011110000001000000000000000000000000011';
xs = sym(x)
xs =
580964352065011715
ys = sym(y)
ys =
94815832244227
isAlways(xs > ys)
ans = logical
1

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

### カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by