README.md (3452B)
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 16-bit integer][integer]. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var toBinaryString = require( '@stdlib/number/uint16/base/to-binary-string' ); 31 ``` 32 33 #### toBinaryString( x ) 34 35 Returns a `string` giving the literal bit representation of an [unsigned 16-bit integer][integer]. 36 37 ```javascript 38 var Uint16Array = require( '@stdlib/array/uint16' ); 39 40 var a = new Uint16Array( [ 1, 4, 9 ] ); 41 42 var str = toBinaryString( a[0] ); 43 // returns '0000000000000001' 44 45 str = toBinaryString( a[1] ); 46 // returns '0000000000000100' 47 48 str = toBinaryString( a[2] ); 49 // returns '0000000000001001' 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 16-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 16-bit integers][integer], the function will accept [floating-point][ieee754] values and represent the values **as if** they are [unsigned 16-bit integers][integer]. Accordingly, care **should** be taken to ensure that **only** nonnegative integer values less than `65536` (`2^16`) are provided. 61 62 ```javascript 63 var str = toBinaryString( 1 ); 64 // returns '0000000000000001' 65 66 str = toBinaryString( 4 ); 67 // returns '0000000000000100' 68 69 str = toBinaryString( 9 ); 70 // returns '0000000000001001' 71 72 str = toBinaryString( 65535 ); 73 // returns '1111111111111111' 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 Uint16Array = require( '@stdlib/array/uint16' ); 90 var MAX_UINT16 = require( '@stdlib/constants/uint16/max' ); 91 var toBinaryString = require( '@stdlib/number/uint16/base/to-binary-string' ); 92 93 var x; 94 var y; 95 var b; 96 var i; 97 98 // Generate random unsigned 16-bit integers... 99 x = new Uint16Array( 100 ); 100 for ( i = 0; i < x.length; i++ ) { 101 x[ i ] = round( randu()*MAX_UINT16 ); 102 } 103 104 // Convert unsigned 16-bit integers to literal bit representations... 105 for ( i = 0; i < x.length; i++ ) { 106 b = toBinaryString( x[i] ); 107 y = parseInt( b, 2 ); 108 console.log( 'x: %d, b: %s, y: %d', x[i], b, y ); 109 } 110 ``` 111 112 </section> 113 114 <!-- /.examples --> 115 116 <section class="links"> 117 118 [integer]: https://en.wikipedia.org/wiki/Integer_%28computer_science%29 119 120 [typed-arrays]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays 121 122 [ecma-262]: http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.19 123 124 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 125 126 </section> 127 128 <!-- /.links -->