How to convert an arbitrary rational to binary digits?
显示 更早的评论
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.
回答(3 个)
Walter Roberson
2017-10-22
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
2017-10-22
dec2base(your_number,2)
3 个评论
Mr M.
2017-10-22
KL
2017-10-22
dec2base(your_number,2)-'0'
Walter Roberson
2017-10-22
dec2base is only for positive integers.
Lai Yit Ming
2021-10-2
0 个投票
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 个评论
Walter Roberson
2021-10-2
that does not appear to be a solution for the Question that was asked.
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!