README.md (2538B)
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 # Bitwise Rotation 22 23 > Bitwise rotation to the right. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var rotr32 = require( '@stdlib/number/uint32/base/rotr' ); 31 ``` 32 33 #### rotr32( x, shift ) 34 35 Performs a bitwise rotation to the right. 36 37 ```javascript 38 var toBinaryStringUint32 = require( '@stdlib/number/uint32/base/to-binary-string' ); 39 40 var x = 2147483649; 41 var bstr = toBinaryStringUint32( x ); 42 // returns '10000000000000000000000000000001' 43 44 var y = rotr32( x, 10 ); 45 // returns 6291456 46 47 bstr = toBinaryStringUint32( y ); 48 // returns '00000000011000000000000000000000' 49 ``` 50 51 </section> 52 53 <!-- /.usage --> 54 55 <section class="notes"> 56 57 ## Notes 58 59 - If provided a `shift` equal to `0`, the function returns the input value. 60 61 ```javascript 62 var y = rotr32( 3, 0 ); 63 // returns 3 64 ``` 65 66 - If provided a `shift` greater than `31`, the function performs a bitwise rotation based on the `5` least significant bits of `shift` (i.e., `shift % 32`). 67 68 ```javascript 69 var shift = 34; // 00000000000000000000000000100010 70 71 var y = rotr( 1, shift ); // 01000000000000000000000000000000 72 // returns 1073741824 73 ``` 74 75 </section> 76 77 <!-- /.notes --> 78 79 <section class="examples"> 80 81 ## Examples 82 83 <!-- eslint no-undef: "error" --> 84 85 ```javascript 86 var toBinaryStringUint32 = require( '@stdlib/number/uint32/base/to-binary-string' ); 87 var MAX_INT = require( '@stdlib/constants/uint32/max' ); 88 var rotr32 = require( '@stdlib/number/uint32/base/rotr' ); 89 90 var HALF; 91 var x; 92 var y; 93 var i; 94 95 for ( i = 0; i < 100; i++ ) { 96 x = i; 97 y = rotr32( x, 10 ); 98 console.log( '%d => %s => %s => %d', x, toBinaryStringUint32( x ), toBinaryStringUint32( y ), y ); 99 } 100 101 HALF = (MAX_INT+1) / 2; 102 for ( i = 0; i < 100; i++ ) { 103 x = HALF + i; 104 y = rotr32( x, 10 ); 105 console.log( '%d => %s => %s => %d', x, toBinaryStringUint32( x ), toBinaryStringUint32( y ), y ); 106 } 107 ``` 108 109 </section> 110 111 <!-- /.examples --> 112 113 <section class="links"> 114 115 </section> 116 117 <!-- /.links -->