main.js (1617B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var lpad = require( '@stdlib/string/left-pad' ); 24 var div2 = require( './div2.js' ); 25 26 27 // VARIABLES // 28 29 var NBITS = 32; 30 31 32 // MAIN // 33 34 /** 35 * Returns a string giving the literal bit representation of an unsigned 32-bit integer. 36 * 37 * @param {uinteger32} x - input value 38 * @returns {BinaryString} bit representation 39 * 40 * @example 41 * var a = new Uint32Array( [ 1 ] ); 42 * var str = toBinaryString( a[0] ); 43 * // returns '00000000000000000000000000000001' 44 * 45 * @example 46 * var a = new Uint32Array( [ 4 ] ); 47 * var str = toBinaryString( a[0] ); 48 * // returns '00000000000000000000000000000100' 49 * 50 * @example 51 * var a = new Uint32Array( [ 9 ] ); 52 * var str = toBinaryString( a[0] ); 53 * // returns '00000000000000000000000000001001' 54 */ 55 function toBinaryString( x ) { 56 var b; 57 58 // Convert the input value to a bit string: 59 b = div2( x ); 60 61 // Left pad the bit string to ensure 32 bits are represented: 62 b = lpad( b, NBITS, '0' ); 63 64 return b; 65 } 66 67 68 // EXPORTS // 69 70 module.exports = toBinaryString;