/*
* Copyright ( C ) 2014 The Android Open Source Project
*
* Licensed under the Apache License , Version 2 . 0 ( the " License " ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an " AS IS " BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
*/
public class ShiftsTest {
public static void expectEquals(int expected, int result) {
if (expected != result) {
throw new Error("Expected: " + expected + ", found: " + result);
}
}
public static void expectEquals(long expected, long result) {
if (expected != result) {
throw new Error("Expected: " + expected + ", found: " + result);
}
}
public static void main() {
testShlInt();
testShlLong();
testShrInt();
testShrLong();
testUShrInt();
testUShrLong();
}
private static void testShlInt() {
expectEquals(48 , $opt$ShlIntConst2(12 ));
expectEquals(12 , $opt$ShlIntConst0(12 ));
expectEquals(-48 , $opt$ShlInt(-12 , 2 ));
expectEquals(1024 , $opt$ShlInt(32 , 5 ));
expectEquals(7 , $opt$ShlInt(7 , 0 ));
expectEquals(14 , $opt$ShlInt(7 , 1 ));
expectEquals(0 , $opt$ShlInt(0 , 30 ));
expectEquals(1073741824 L, $opt$ShlInt(1 , 30 ));
expectEquals(Integer.MIN_VALUE, $opt$ShlInt(1 , 31 )); // overflow
expectEquals(Integer.MIN_VALUE, $opt$ShlInt(1073741824 , 1 )); // overflow
expectEquals(1073741824 , $opt$ShlInt(268435456 , 2 ));
// Only the 5 lower bits should be used for shifting (& 0x1f).
expectEquals(7 , $opt$ShlInt(7 , 32 )); // 32 & 0x1f = 0
expectEquals(14 , $opt$ShlInt(7 , 33 )); // 33 & 0x1f = 1
expectEquals(32 , $opt$ShlInt(1 , 101 )); // 101 & 0x1f = 5
expectEquals(Integer.MIN_VALUE, $opt$ShlInt(1 , -1 )); // -1 & 0x1f = 31
expectEquals(14 , $opt$ShlInt(7 , -31 )); // -31 & 0x1f = 1
expectEquals(7 , $opt$ShlInt(7 , -32 )); // -32 & 0x1f = 0
expectEquals(-536870912 , $opt$ShlInt(7 , -3 )); // -3 & 0x1f = 29
expectEquals(Integer.MIN_VALUE, $opt$ShlInt(7 , Integer.MAX_VALUE));
expectEquals(7 , $opt$ShlInt(7 , Integer.MIN_VALUE));
}
private static void testShlLong() {
expectEquals(48 L, $opt$ShlLongConst2(12 L));
expectEquals(12 L, $opt$ShlLongConst0(12 L));
expectEquals(-48 L, $opt$ShlLong(-12 L, 2 ));
expectEquals(1024 L, $opt$ShlLong(32 L, 5 ));
expectEquals(7 L, $opt$ShlLong(7 L, 0 ));
expectEquals(14 L, $opt$ShlLong(7 L, 1 ));
expectEquals(0 L, $opt$ShlLong(0 L, 30 ));
expectEquals(1073741824 L, $opt$ShlLong(1 L, 30 ));
expectEquals(2147483648 L, $opt$ShlLong(1 L, 31 ));
expectEquals(2147483648 L, $opt$ShlLong(1073741824 L, 1 ));
// Long shifts can use up to 6 lower bits.
expectEquals(4294967296 L, $opt$ShlLong(1 L, 32 ));
expectEquals(60129542144 L, $opt$ShlLong(7 L, 33 ));
expectEquals(Long .MIN_VALUE, $opt$ShlLong(1 L, 63 )); // overflow
// Only the 6 lower bits should be used for shifting (& 0x3f).
expectEquals(7 L, $opt$ShlLong(7 L, 64 )); // 64 & 0x3f = 0
expectEquals(14 L, $opt$ShlLong(7 L, 65 )); // 65 & 0x3f = 1
expectEquals(137438953472 L, $opt$ShlLong(1 L, 101 )); // 101 & 0x3f = 37
expectEquals(Long .MIN_VALUE, $opt$ShlLong(1 L, -1 )); // -1 & 0x3f = 63
expectEquals(14 L, $opt$ShlLong(7 L, -63 )); // -63 & 0x3f = 1
expectEquals(7 L, $opt$ShlLong(7 L, -64 )); // -64 & 0x3f = 0
expectEquals(2305843009213693952 L, $opt$ShlLong(1 L, -3 )); // -3 & 0x3f = 61
expectEquals(Long .MIN_VALUE, $opt$ShlLong(7 L, Integer.MAX_VALUE));
expectEquals(7 L, $opt$ShlLong(7 L, Integer.MIN_VALUE));
// Exercise some special cases handled by backends/simplifier.
expectEquals(24 L, $opt$ShlLongConst1(12 L));
expectEquals(0 x2345678900000000L, $opt$ShlLongConst32(0 x123456789L));
expectEquals(0 x2490249000000000L, $opt$ShlLongConst33(0 x12481248L));
expectEquals(0 x4920492000000000L, $opt$ShlLongConst34(0 x12481248L));
expectEquals(0 x9240924000000000L, $opt$ShlLongConst35(0 x12481248L));
}
private static void testShrInt() {
expectEquals(3 , $opt$ShrIntConst2(12 ));
expectEquals(12 , $opt$ShrIntConst0(12 ));
expectEquals(-3 , $opt$ShrInt(-12 , 2 ));
expectEquals(1 , $opt$ShrInt(32 , 5 ));
expectEquals(7 , $opt$ShrInt(7 , 0 ));
expectEquals(3 , $opt$ShrInt(7 , 1 ));
expectEquals(0 , $opt$ShrInt(0 , 30 ));
expectEquals(0 , $opt$ShrInt(1 , 30 ));
expectEquals(-1 , $opt$ShrInt(-1 , 30 ));
expectEquals(0 , $opt$ShrInt(Integer.MAX_VALUE, 31 ));
expectEquals(-1 , $opt$ShrInt(Integer.MIN_VALUE, 31 ));
// Only the 5 lower bits should be used for shifting (& 0x1f).
expectEquals(7 , $opt$ShrInt(7 , 32 )); // 32 & 0x1f = 0
expectEquals(3 , $opt$ShrInt(7 , 33 )); // 33 & 0x1f = 1
expectEquals(0 , $opt$ShrInt(1 , -1 )); // -1 & 0x1f = 31
expectEquals(3 , $opt$ShrInt(7 , -31 )); // -31 & 0x1f = 1
expectEquals(7 , $opt$ShrInt(7 , -32 )); // -32 & 0x1f = 0
expectEquals(-4 , $opt$ShrInt(Integer.MIN_VALUE, -3 )); // -3 & 0x1f = 29
expectEquals(0 , $opt$ShrInt(7 , Integer.MAX_VALUE));
expectEquals(7 , $opt$ShrInt(7 , Integer.MIN_VALUE));
}
private static void testShrLong() {
expectEquals(3 L, $opt$ShrLongConst2(12 L));
expectEquals(12 L, $opt$ShrLongConst0(12 L));
expectEquals(-3 L, $opt$ShrLong(-12 L, 2 ));
expectEquals(1 , $opt$ShrLong(32 , 5 ));
expectEquals(7 L, $opt$ShrLong(7 L, 0 ));
expectEquals(3 L, $opt$ShrLong(7 L, 1 ));
expectEquals(0 L, $opt$ShrLong(0 L, 30 ));
expectEquals(0 L, $opt$ShrLong(1 L, 30 ));
expectEquals(-1 L, $opt$ShrLong(-1 L, 30 ));
expectEquals(1 L, $opt$ShrLong(1073741824 L, 30 ));
expectEquals(1 L, $opt$ShrLong(2147483648 L, 31 ));
expectEquals(1073741824 L, $opt$ShrLong(2147483648 L, 1 ));
// Long shifts can use up to 6 lower bits.
expectEquals(1 L, $opt$ShrLong(4294967296 L, 32 ));
expectEquals(7 L, $opt$ShrLong(60129542144 L, 33 ));
expectEquals(0 L, $opt$ShrLong(Long .MAX_VALUE, 63 ));
expectEquals(-1 L, $opt$ShrLong(Long .MIN_VALUE, 63 ));
// Only the 6 lower bits should be used for shifting (& 0x3f).
expectEquals(7 L, $opt$ShrLong(7 L, 64 )); // 64 & 0x3f = 0
expectEquals(3 L, $opt$ShrLong(7 L, 65 )); // 65 & 0x3f = 1
expectEquals(-1 L, $opt$ShrLong(Long .MIN_VALUE, -1 )); // -1 & 0x3f = 63
expectEquals(3 L, $opt$ShrLong(7 L, -63 )); // -63 & 0x3f = 1
expectEquals(7 L, $opt$ShrLong(7 L, -64 )); // -64 & 0x3f = 0
expectEquals(1 L, $opt$ShrLong(2305843009213693952 L, -3 )); // -3 & 0x3f = 61
expectEquals(-1 L, $opt$ShrLong(Integer.MIN_VALUE, -3 )); // -3 & 0x1f = 29
expectEquals(0 L, $opt$ShrLong(7 L, Integer.MAX_VALUE));
expectEquals(7 L, $opt$ShrLong(7 L, Integer.MIN_VALUE));
}
private static void testUShrInt() {
expectEquals(3 , $opt$UShrIntConst2(12 ));
expectEquals(12 , $opt$UShrIntConst0(12 ));
expectEquals(1073741821 , $opt$UShrInt(-12 , 2 ));
expectEquals(1 , $opt$UShrInt(32 , 5 ));
expectEquals(7 , $opt$UShrInt(7 , 0 ));
expectEquals(3 , $opt$UShrInt(7 , 1 ));
expectEquals(0 , $opt$UShrInt(0 , 30 ));
expectEquals(0 , $opt$UShrInt(1 , 30 ));
expectEquals(3 , $opt$UShrInt(-1 , 30 ));
expectEquals(0 , $opt$UShrInt(Integer.MAX_VALUE, 31 ));
expectEquals(1 , $opt$UShrInt(Integer.MIN_VALUE, 31 ));
// Only the 5 lower bits should be used for shifting (& 0x1f).
expectEquals(7 , $opt$UShrInt(7 , 32 )); // 32 & 0x1f = 0
expectEquals(3 , $opt$UShrInt(7 , 33 )); // 33 & 0x1f = 1
expectEquals(0 , $opt$UShrInt(1 , -1 )); // -1 & 0x1f = 31
expectEquals(3 , $opt$UShrInt(7 , -31 )); // -31 & 0x1f = 1
expectEquals(7 , $opt$UShrInt(7 , -32 )); // -32 & 0x1f = 0
expectEquals(4 , $opt$UShrInt(Integer.MIN_VALUE, -3 )); // -3 & 0x1f = 29
expectEquals(0 , $opt$UShrInt(7 , Integer.MAX_VALUE));
expectEquals(7 , $opt$UShrInt(7 , Integer.MIN_VALUE));
}
private static void testUShrLong() {
expectEquals(3 L, $opt$UShrLongConst2(12 L));
expectEquals(12 L, $opt$UShrLongConst0(12 L));
expectEquals(4611686018427387901 L, $opt$UShrLong(-12 L, 2 ));
expectEquals(1 , $opt$UShrLong(32 , 5 ));
expectEquals(7 L, $opt$UShrLong(7 L, 0 ));
expectEquals(3 L, $opt$UShrLong(7 L, 1 ));
expectEquals(0 L, $opt$UShrLong(0 L, 30 ));
expectEquals(0 L, $opt$UShrLong(1 L, 30 ));
expectEquals(17179869183 L, $opt$UShrLong(-1 L, 30 ));
expectEquals(1 L, $opt$UShrLong(1073741824 L, 30 ));
expectEquals(1 L, $opt$UShrLong(2147483648 L, 31 ));
expectEquals(1073741824 L, $opt$UShrLong(2147483648 L, 1 ));
// Long shifts can use use up to 6 lower bits.
expectEquals(1 L, $opt$UShrLong(4294967296 L, 32 ));
expectEquals(7 L, $opt$UShrLong(60129542144 L, 33 ));
expectEquals(0 L, $opt$UShrLong(Long .MAX_VALUE, 63 ));
expectEquals(1 L, $opt$UShrLong(Long .MIN_VALUE, 63 ));
// Only the 6 lower bits should be used for shifting (& 0x3f).
expectEquals(7 L, $opt$UShrLong(7 L, 64 )); // 64 & 0x3f = 0
expectEquals(3 L, $opt$UShrLong(7 L, 65 )); // 65 & 0x3f = 1
expectEquals(1 L, $opt$UShrLong(Long .MIN_VALUE, -1 )); // -1 & 0x3f = 63
expectEquals(3 L, $opt$UShrLong(7 L, -63 )); // -63 & 0x3f = 1
expectEquals(7 L, $opt$UShrLong(7 L, -64 )); // -64 & 0x3f = 0
expectEquals(1 L, $opt$UShrLong(2305843009213693952 L, -3 )); // -3 & 0x3f = 61
expectEquals(4 L, $opt$UShrLong(Long .MIN_VALUE, -3 )); // -3 & 0x3f = 61
expectEquals(0 L, $opt$UShrLong(7 L, Integer.MAX_VALUE));
expectEquals(7 L, $opt$UShrLong(7 L, Integer.MIN_VALUE));
}
static int $opt$ShlInt(int value, int distance) {
return value << distance;
}
static long $opt$ShlLong(long value, int distance) {
return value << distance;
}
static int $opt$ShrInt(int value, int distance) {
return value >> distance;
}
static long $opt$ShrLong(long value, int distance) {
return value >> distance;
}
static int $opt$UShrInt(int value, int distance) {
return value >>> distance;
}
static long $opt$UShrLong(long value, int distance) {
return value >>> distance;
}
static int $opt$ShlIntConst2(int value) {
return value << 2 ;
}
static long $opt$ShlLongConst2(long value) {
return value << 2 ;
}
static int $opt$ShrIntConst2(int value) {
return value >> 2 ;
}
static long $opt$ShrLongConst2(long value) {
return value >> 2 ;
}
static int $opt$UShrIntConst2(int value) {
return value >>> 2 ;
}
static long $opt$UShrLongConst2(long value) {
return value >>> 2 ;
}
static int $opt$ShlIntConst0(int value) {
return value << 0 ;
}
static long $opt$ShlLongConst0(long value) {
return value << 0 ;
}
static int $opt$ShrIntConst0(int value) {
return value >> 0 ;
}
static long $opt$ShrLongConst0(long value) {
return value >> 0 ;
}
static int $opt$UShrIntConst0(int value) {
return value >>> 0 ;
}
static long $opt$UShrLongConst0(long value) {
return value >>> 0 ;
}
static long $opt$ShlLongConst1(long value) {
return value << 1 ;
}
static long $opt$ShlLongConst32(long value) {
return value << 32 ;
}
static long $opt$ShlLongConst33(long value) {
return value << 33 ;
}
static long $opt$ShlLongConst34(long value) {
return value << 34 ;
}
static long $opt$ShlLongConst35(long value) {
return value << 35 ;
}
}
Messung V0.5 in Prozent C=87 H=98 G=92
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-06-29)
¤
*© Formatika GbR, Deutschland