Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <input type="text" id="input" />
<button id="print">Print</button>
  <div>Sign bit: <span id="sign"></span></div>
  <div>Exponent bits: <span id="exponent"></span></div>
  <div>Significant bits: <span id="significant"></span></div>
  <div>Scientific Binary Representation: <span id="output"></span></div>  
              document.getElementById('print').addEventListener('click', function() {
  var input = document.getElementById('input'),
      output = document.getElementById('output'),
      exponent = document.getElementById('exponent'),
      significant = document.getElementById('significant'),
      sign = document.getElementById('sign');
  var n = parseFloat(input.value);  
  var res = toMathString(n);
  output.innerText = res[0];
  sign.innerText = res[1];
  exponent.innerText = res[2];
  significant.innerText = res[3];

// assuming BigEndian
 * getting bit sequence of an input number
function toBits(n, bitCount) {
	var x = n;
	var bs = [];
	for (var b = 0; b < bitCount; b++) {
		x = n >> b;		
		bs.push(x & 0x01);
	return bs.reverse();

 * nicely print a binary number. omit trailing 0 after the dot.
function toBinaryStr(bits) {	
	var suffix = bits.slice(1).join('').replace(/0+$/, '');
	suffix = suffix.length == 0? '0': suffix;
	return bits[0] + '.' + suffix;

function toMathString(n) {
	var buffer = new ArrayBuffer(4);
	new DataView(buffer).setFloat32(0, n);
	var bytes = new Uint8Array(buffer);

	// 0 - positive, 1 - negative
  var signBit = (bytes[0] >> 7);
	var sign = signBit > 0 ? '-': '';
	var exp = (bytes[0] << 1) | (bytes[1] >> 7);
	// expected: 126 , in binary 01111110
	// for float32, minus 127 from raw bits to get signed value
	var expVal = exp - 127;
	// expected: -1
	var expSign = expVal > 0? '': '-';

	// the most significant bit is 1 and is ommited
	// So we put it back here
	var sig = (((bytes[1] & 0x7F) | 0x80) << 16) | (bytes[2] << 8) | bytes[3];

	// expected: 8388608 , which is 10...0 (23 zeros)
	var mathRep = toBinaryStr(toBits(sig, 24)) + 'E' 
			+ expSign 
			+ toBits(Math.abs(expVal), 8).join('').replace(/^0+/,'');
	return [mathRep, signBit, 
          toBits(exp, 8).join(''), 
          toBits(sig, 23).join('')];
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.