de2bi
(非推奨) 10 進数から基数 P への変換
説明
例
10 進数から基数 2 への変換
この例では、10 進数を基数 2 の等価の 2 進数に変換する方法を示します。
d_array = [1 2 3 4];
関数 de2bi
を使用して、10 進数の配列を 2 進数に変換します。左端の要素を最上位桁として指定し、目的の列の数を 5 に設定します。出力は、各行が入力の 10 進数値に対応する 4 行 5 列の行列になります。d_array
の最大の 10 進数値は 3 列で表現できるため、de2bi
は行列の指定された最上位ビット側にゼロの列を 2 列付加します。指定する列が少なすぎると変換は失敗します。
b_array = de2bi(d_array,5,'left-msb')
b_array = 4×5
0 0 0 0 1
0 0 0 1 0
0 0 0 1 1
0 0 1 0 0
b_array = de2bi(d_array,5,'right-msb')
b_array = 4×5
1 0 0 0 0
0 1 0 0 0
1 1 0 0 0
0 0 1 0 0
列数を指定しない場合、列数は入力の最大の 10 進数を表現するために必要な数になります。
b_array = de2bi(d_array,'left-msb')
b_array = 4×3
0 0 1
0 1 0
0 1 1
1 0 0
左端を最上位ビットに指定する場合の出力行は以下に対応します。
b_array = de2bi(d_array,'right-msb')
b_array = 4×3
1 0 0
0 1 0
1 1 0
0 0 1
右端を最上位ビットに指定する出力行は以下に対応します。
入力引数
d
— 10 進数入力
非負の整数 | ベクトル | 行列
10 進数入力。非負の整数、ベクトル、または行列として指定します。d
が行列の場合、列ベクトル d(:)
と同様に扱われます。
メモ
変換が正確に行われるようにするには、d
は 252 以下でなければなりません。
データ型: double
| single
| integer
| fi
n
— 出力列の数
正の整数スカラー
正のスカラーで指定される出力列の数。必要に応じて、d
のバイナリ表現には、ゼロが付加されます。
データ型: double
| single
flg
— MSB フラグ
'right-msb'
(既定値) | 'left-msb'
MSB フラグ。'right-msb'
または 'left-msb'
として指定します。
'right-msb'
–– バイナリ出力b
の右 (または最後) の列を最上位ビット (または最上位桁) として示します。'left-msb'
–– バイナリ出力b
の左 (または最初) の列を最上位ビット (または最上位桁) として示します。
データ型: char
| string
拡張機能
C/C++ コード生成
MATLAB® Coder™ を使用して C および C++ コードを生成します。
バージョン履歴
R2006a より前に導入R2021b: 非推奨
de2bi
の代わりに int2bit
を使用してください。数値表現を 10 進数から 2 以外の基数に変換するには、dec2base
を使用します。
次の表に記載するコードは、推奨される関数を使用した、各種入力の 10 進数から 2 進数への変換を示しています。
非推奨の機能 | 推奨される代替案 |
---|---|
% Default (left MSB) n = randi([1 100]); % Number of integers bpi = 3; % Bits per integer x = randi([0,2^bpi-1],n,1); y = reshape(de2bi(x,bpi,'left-msb')',[],1) | % Default (left MSB) n = randi([1 100]); % Number of integers bpi = 3; % Bits per integer x = randi([0,2^bpi-1],n,1); y = int2bit(x,bpi) |
% Default vector (or scalar) input
x = [4 5 9];
y = de2bi(x) | % Default vector (or scalar) input
x = [4 5 9];
y = int2bit(x,ceil(log2(max(x) + 1)), 0)' |
% Right MSB n = randi([1 100]); % Number of integers bpi = 5; % Bits per integer x = randi([0,2^bpi-1],n,1); y = reshape(de2bi(x,bpi,'right-msb')',[],1) | % Right MSB n = randi([1 100]); % Number of integers bpi = 5; % Bits per integer x = randi([0,2^bpi-1],n,1); y = int2bit(x,bpi,false) |
% Right MSB, signed input n = randi([1 100]); % Number of integers bpi = 8; % Bits per integer N = 2^bpi; x = randi([-N/2,N/2-1],n,1); y = reshape(de2bi(x+(x<0)*N,bpi,'right-msb')',[],1) | % Right MSB, signed input n = randi([1 100]); % Number of integers bpi = 8; % Bits per integer N = 2^bpi; x = randi([-N/2,N/2-1],n,1); y = int2bit(x+(x<0)*N,bpi,false) |
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)