main.js (1839B)
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 toUint32 = require( './../../../../int32/base/to-uint32' ); 24 25 26 // VARIABLES // 27 28 // Shift mask: 00000000000000000000000000011111 29 var MAX_SHIFT = toUint32( 31 ); 30 31 32 // MAIN // 33 34 /** 35 * Performs a bitwise rotation to the right. 36 * 37 * ## References 38 * 39 * - [Safe, Efficient, and Portable Rotate in C/C++](http://blog.regehr.org/archives/1063) 40 * - [Best practices for rotates in C/C++](https://stackoverflow.com/a/776523/224132) 41 * - [Near constant time rotate that does not violate the standards](https://stackoverflow.com/a/31488147/224132) 42 * 43 * 44 * @param {uinteger32} x - unsigned integer 45 * @param {uinteger32} shift - number of bits to shift 46 * @returns {uinteger32} shifted integer 47 * 48 * @example 49 * var toBinaryStringUint32 = require( '@stdlib/number/uint32/base/to-binary-string' ); 50 * 51 * var bstr = toBinaryStringUint32( 2147483649 ); 52 * // returns '10000000000000000000000000000001' 53 * 54 * var x = rotr32( 2147483649, 10 ); 55 * // returns 6291456 56 * 57 * bstr = toBinaryStringUint32( x ); 58 * // returns '00000000011000000000000000000000' 59 */ 60 function rotr32( x, shift ) { 61 shift &= MAX_SHIFT; 62 return toUint32( ( x >>> shift ) | (x << ((-shift) & MAX_SHIFT)) ); 63 } 64 65 66 // EXPORTS // 67 68 module.exports = rotr32;