Skip to the content of the web site.

Numeric-to-Binary Conversion

These pages document the numeric-to-binary functions presented here. A floating-point number or a double-precision floating-point number may be converted to a binary representation using these functions. In addition, there are functions for all of the integer types. For example, the output of

#include <iostream>
#include "num2bin.h"

using namespace std;
using namespace ca_uwaterloo_alumni_dwharder;

int main() {
	float x = 3.14;
	double y = 3.14;

	cout << num2bin( x ) << endl;
	cout << num2bin( y ) << endl;

	return 0;
}

are the two strings

0    10000000-10010001111010111000011
0 10000000000-1001000111101011100001010001111010111000010100011111

The first bit is the sign bit, the next bits up to the '-' are the exponents, while the remaining bits are the mantissa. These two numbers represent the binary numbers +11.00100011110101110000112 and +11.0010001111010111000010100011110101110000101000111112, respectively.

The header and source files is in the source code directory (left menu).

The mass of the earth is approximately 5.9742×1024 kg which may be printed as

0    11010001-00111100010001011000001
0 10001010001-0011110001000101100000011111101011111000111101010000

The mass of a free neutron is approximately 1.67262158×10-27 kg (reference) which may be printed as

0    00100110-00001001000010011001100
0 01110100110-0000100100001001100101111000110011111010111000010011