num2bin

Expression of all numbers in a variety of binary forms.
21 download
Aggiornato 3 gen 2023

Visualizza la licenza

num2bin is a tool that performs extraction of the bits of a number in MATLAB. It returns a struct that contains multiple fields, which show the number in all forms.
Single and double precision numbers are resolved, as well as negative numbers.
num2bin(single(-0.1))
ans =
struct with fields:
Class: 'single'
Sign: -1
Exponent: -4
Mantissa: '110011001100110011001101'
BinaryExpansion: [-4 -5 -8 -9 -12 -13 -16 -17 -20 -21 -24 -25 -27]
BiSci: '-1.10011001100110011001101 B-4'
BiCimal: '-0.000110011001100110011001101'
num2bin(1.23456789e6)
ans =
struct with fields:
Class: 'double'
Sign: 1
Exponent: 20
Mantissa: '10010110101101000011111100011110101110000101000111101'
BinaryExpansion: [20 17 15 14 12 10 9 7 2 1 0 -1 -2 -3 -7 -8 -9 -10 -12 -14 -15 -16 -21 -23 -27 -28 -29 -30 -32]
BiSci: '1.0010110101101000011111100011110101110000101000111101 B20'
BiCimal: '100101101011010000111.11100011110101110000101000111101'
Integer classes are properly handled: uint8, uint16, uint32, uint64 as well as int8, int16, int32, int64, even logical.
num2bin(uint32(123456))
ans =
struct with fields:
Class: 'uint32'
Sign: 1
Mantissa: '11110001001000000'
Exponent: 16
BinaryExpansion: [16 15 14 13 9 6]
BiSci: '1.1110001001000000 B16'
BiCimal: '11110001001000000'
Zero has a sign of 0.
num2bin(false)
ans =
struct with fields:
Class: 'logical'
Sign: 0
Exponent: 0
Mantissa: '0'
BinaryExpansion: []
BiSci: '0'
BiCimal: '0'
eps
num2bin(eps)
ans =
struct with fields:
Class: 'double'
Sign: 1
Exponent: -52
Mantissa: '10000000000000000000000000000000000000000000000000000'
BinaryExpansion: -52
BiSci: '1.0000000000000000000000000000000000000000000000000000 B-52'
BiCimal: '0.00000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000'
Denormal numbers, thus smaller than realmin, but not quite small enough to underflow into a zero are extracted. The smallest such number representable as a double would be realmin/2^52. Beyond that point we would see an underflow to zero.
num2bin(realmin/2^52)
ans =
struct with fields:
Class: 'double'
Sign: 1
Exponent: -1023
Mantissa: '00000000000000000000000000000000000000000000000000001'
BinaryExpansion: -1074
BiSci: '0.0000000000000000000000000000000000000000000000000001 B-1022'
BiCimal: '0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001'
Exact powers of 2 are representable exactly.
num2bin(2^-100)
ans =
struct with fields:
Class: 'double'
Sign: 1
Exponent: -100
Mantissa: '10000000000000000000000000000000000000000000000000000'
BinaryExpansion: -100
BiSci: '1.0000000000000000000000000000000000000000000000000000 B-100'
BiCimal: '0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000'
Note that 1/3 is a repeating decimal in base 10, but also a repeating decimal in base 2. In base 2, 1/3 repeats the binary "digits": '01' forever.
num2bin(1/3)
ans =
struct with fields:
Class: 'double'
Sign: 1
Exponent: -2
Mantissa: '10101010101010101010101010101010101010101010101010101'
BinaryExpansion: [-2 -4 -6 -8 -10 -12 -14 -16 -18 -20 -22 -24 -26 -28 -30 -32 -34 -36 -38 -40 -42 -44 -46 -48 -50 -52 -54]
BiSci: '1.0101010101010101010101010101010101010101010101010101 B-2'
BiCimal: '0.010101010101010101010101010101010101010101010101010101'
Similarly, 1/5 has a finite expansion in base 10 (thus 0.2), yet is infinitely repeating in base 2.
num2bin(1/5)
ans =
struct with fields:
Class: 'double'
Sign: 1
Exponent: -3
Mantissa: '11001100110011001100110011001100110011001100110011010'
BinaryExpansion: [-3 -4 -7 -8 -11 -12 -15 -16 -19 -20 -23 -24 -27 -28 -31 -32 -35 -36 -39 -40 -43 -44 -47 -48 -51 -52 -54]
BiSci: '1.1001100110011001100110011001100110011001100110011010 B-3'
BiCimal: '0.0011001100110011001100110011001100110011001100110011010'
inf - an overflow, as a single, stored as 2^128. As a double, effectively 2^1024.
num2bin(single(-inf))
ans =
struct with fields:
Class: 'single'
Sign: -1
Exponent: 128
Mantissa: '100000000000000000000000'
BinaryExpansion: 128
BiSci: '-1.00000000000000000000000 B128'
BiCimal: '-1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
NaN? what do you expect to see in this case? It is not a number.
num2bin(NaN)
ans =
struct with fields:
Class: 'double'
Sign: NaN
Exponent: NaN
Mantissa: '1000000000000000000000000000000000000000000000000000'
BinaryExpansion: 'NaN'
BiSci: 'NaN'
BiCimal: 'NaN'

Cita come

John D'Errico (2024). num2bin (https://www.mathworks.com/matlabcentral/fileexchange/122792-num2bin), MATLAB Central File Exchange. Recuperato .

Compatibilità della release di MATLAB
Creato con R2022b
Compatibile con qualsiasi release
Compatibilità della piattaforma
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
Versione Pubblicato Note della release
1.0.1

Just a fix in the documentation.

1.0.0