How to convert an arbitrary rational to binary digits?

1 visualización (últimos 30 días)
Mr M.
Mr M. el 22 de Oct. de 2017
Comentada: Walter Roberson el 2 de Oct. de 2021
I have a number n1/n2, where n1 and n2 are integers. I would like to convert to base-2 and get the digits in order.

Respuestas (3)

Walter Roberson
Walter Roberson el 22 de Oct. de 2017
bits = reshape((dec2bin(sscanf(num2hex(double(n1)/double(n2)),'%8x'),32) - '0').',1,[]);
... under at least one reasonable interpretation of what "bits" are appropriate.
For example, if n1 = 1 and n2 = 18446744073709551615 then one exact binary representation is an infinite repeating sequence of sixty-three 0's followed by a 1...

KL
KL el 22 de Oct. de 2017
dec2base(your_number,2)
  3 comentarios
KL
KL el 22 de Oct. de 2017
dec2base(your_number,2)-'0'
Walter Roberson
Walter Roberson el 22 de Oct. de 2017
dec2base is only for positive integers.

Iniciar sesión para comentar.


Lai Yit Ming
Lai Yit Ming el 2 de Oct. de 2021
Binary Converter Program - Write a program to accept a single binary number of up to 8 bits and display the equivalent in decimal. Using matlab
  1 comentario
Walter Roberson
Walter Roberson el 2 de Oct. de 2021
that does not appear to be a solution for the Question that was asked.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by