README.md (3512B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Binary String 22 23 > Return a string giving the literal bit representation of an [unsigned 32-bit integer][integer]. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var toBinaryString = require( '@stdlib/number/uint32/base/to-binary-string' ); 31 ``` 32 33 #### toBinaryString( x ) 34 35 Returns a `string` giving the literal bit representation of an [unsigned 32-bit integer][integer]. 36 37 ```javascript 38 var Uint32Array = require( '@stdlib/array/uint32' ); 39 40 var a = new Uint32Array( [ 1, 4, 9 ] ); 41 42 var str = toBinaryString( a[0] ); 43 // returns '00000000000000000000000000000001' 44 45 str = toBinaryString( a[1] ); 46 // returns '00000000000000000000000000000100' 47 48 str = toBinaryString( a[2] ); 49 // returns '00000000000000000000000000001001' 50 ``` 51 52 </section> 53 54 <!-- /.usage --> 55 56 <section class="notes"> 57 58 ## Notes 59 60 - Except for [typed arrays][typed-arrays], JavaScript does **not** provide native user support for [unsigned 32-bit integers][integer]. According to the [ECMAScript standard][ecma-262], `number` values correspond to [double-precision floating-point numbers][ieee754]. While this function is intended for [unsigned 32-bit integers][integer], the function will accept [floating-point][ieee754] values and represent the values **as if** they are [unsigned 32-bit integers][integer]. Accordingly, care **should** be taken to ensure that **only** nonnegative integer values less than `4,294,967,296` (`2^32`) are provided. 61 62 ```javascript 63 var str = toBinaryString( 1 ); 64 // returns '00000000000000000000000000000001' 65 66 str = toBinaryString( 4 ); 67 // returns '00000000000000000000000000000100' 68 69 str = toBinaryString( 9 ); 70 // returns '00000000000000000000000000001001' 71 72 str = toBinaryString( 4294967295 ); 73 // returns '11111111111111111111111111111111' 74 ``` 75 76 </section> 77 78 <!-- /.notes --> 79 80 <section class="examples"> 81 82 ## Examples 83 84 <!-- eslint no-undef: "error" --> 85 86 ```javascript 87 var randu = require( '@stdlib/random/base/randu' ); 88 var round = require( '@stdlib/math/base/special/round' ); 89 var Uint32Array = require( '@stdlib/array/uint32' ); 90 var toBinaryString = require( '@stdlib/number/uint32/base/to-binary-string' ); 91 92 var x; 93 var y; 94 var b; 95 var i; 96 97 // Generate random unsigned 32-bit integers... 98 x = new Uint32Array( 100 ); 99 for ( i = 0; i < x.length; i++ ) { 100 x[ i ] = round( randu()*1.0e5 ); 101 } 102 103 // Convert unsigned 32-bit integers to literal bit representations... 104 for ( i = 0; i < x.length; i++ ) { 105 b = toBinaryString( x[i] ); 106 y = parseInt( b, 2 ); 107 console.log( 'x: %d, b: %s, y: %d', x[i], b, y ); 108 } 109 ``` 110 111 </section> 112 113 <!-- /.examples --> 114 115 <section class="links"> 116 117 [integer]: https://en.wikipedia.org/wiki/Integer_%28computer_science%29 118 119 [typed-arrays]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays 120 121 [ecma-262]: http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.19 122 123 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 124 125 </section> 126 127 <!-- /.links -->