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