bit-shift

Why is -1 zero fill right shift 1=2147483647 for integers in Java?

你说的曾经没有我的故事 提交于 2019-12-10 02:06:13
问题 For the program below: public class ZeroFillRightShift { public static void main(String args[]) { int x = -1; int y = x>>>1; System.out.println("x = " + x); System.out.println("y = " + y); } I get the output as follows: x = -1 y = 2147483647 The result that I got for -1>>>1 is 2147483647. If it’s the sign bit that has to be shifted, as I learned, the result should be 1073741824. Why is it 2147483647 then? The following image illustrates my problem more clearly: 回答1: The unsigned right shift

n is negative, positive or zero? return 1, 2, or 4

↘锁芯ラ 提交于 2019-12-09 07:26:55
问题 I'm building a PowerPC interpreter, and it works quite well. In the Power architecture the condition register CR0 (EFLAGS on x86) is updated on almost any instruction. It is set like this. The value of CR0 is 1, if the last result was negative, 2 if the last result was positive, 4 otherwise. My first naive method to interpret this is: if (n < 0) cr0 = 1 else if (n > 0) cr0 = 2; else cr0 = 4; However I understand that all those branches won't be optimal, being run millions of times per second.

How can I get a result larger than 2^32 from shl?

夙愿已清 提交于 2019-12-08 21:38:46
问题 Declaration... const n = 2 shl 33 will set constant n to value 4 without any compiler complaint! Also... Caption := IntToStr(2 shl 33); ...return 4 instead 8589934592. It looks like the compiler calculates like this: 2 shl 33 = 2 shl (33 and $1F) = 4 But without any warning or overflow. The problem remains if we declare: const n: int64 = 2 shl 33; The number in constant is still 4 instead 8589934592. Any reasonable work around? 回答1: You're looking for the wrong results, according to both the

Why does bit-shifting an int upwards produce a negative number?

江枫思渺然 提交于 2019-12-08 17:45:05
问题 I am new to bit manipulations tricks and I wrote a simple code to see the output of doing single bit shifts on a single number viz. 2 #include <iostream> int main(int argc, char *argv[]) { int num=2; do { std::cout<<num<<std::endl; num=num<<1;//Left shift by 1 bit. } while (num!=0); return 0; } The output of this is the following. 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456

How to make (1 << 9) pass MISRA? [duplicate]

橙三吉。 提交于 2019-12-08 17:37:56
问题 This question already has answers here : MISRA C:2004, error with bit shifting (3 answers) Closed 5 years ago . We are using Parasoft Static Analysis with MISRA C 2004 checker turned on. The software is an embedded system. We like to describe constants as follows: [1] #define MOTOR_ON (1 << 9) This would show that the 9th bit in the register should be a 1 to turn on the motor. The expression is failing MISRA, so we changed it: [2] #define MOTOR_ON (1U << 9U) The changes convert to unsigned

Why am I getting strange results bit-shifting by a negative value?

夙愿已清 提交于 2019-12-08 16:14:57
问题 This question is NOT a duplicate of this question. I came across a situation where I might have had to left-shift a (positive) number by a negative value, i.e., 8 << -1. In that case, I would expect the result to be 4, but I'd never done this before. So I made up a little test program to verify my hypothesis: for (int i = -8; i <= 4; i++) Console.WriteLine("i = {0}, 8 << {0} = {1}", i, 8 << i); which to my shock and surprise gave me the following output: i = -8, 8 << -8 = 134217728 i = -7, 8

Why does -1 >> 1 and 0xFFFFFFFF >> 1 produce different results?

霸气de小男生 提交于 2019-12-08 16:08:59
问题 I am trying to make a test to tell whether my PC performs arithmetic or logical right shift by right-shifting hexadecimal FFFFFFFF by 1 . I know that an integer -1 reads as FFFFFFFF in hexadecimal since it is the two's complement of 1 . Right-shifting -1 by 1 results in FFFFFFFF and shows the PC performed arithmetic right shift. But if I just type in 0xFFFFFFFF >> 1 , it resulted in 7FFFFFFF and shows that the PC performed logical right shift instead. Why did that happen? See for the code

What are the exact semantics of Rust's shift operators?

ⅰ亾dé卋堺 提交于 2019-12-08 15:57:48
问题 I tried to find exact information about how the << and >> operators work on integers, but I couldn't find a clear answer (the documentation is not that great in that regard). There are two parts of the semantics that are not clear to me. First, what bits are "shifted in"? Zeroes are shifted in from one side (i.e. 0b1110_1010u8 << 4 == 0b1010_0000u8 ), or the bits rotate (i.e. 0b1110_1010u8 << 4 == 0b1010_1110u8 ), or it's unspecified (like overflowing behavior of integers is unspecified), or

Does C++20 well-define left shift for signed integers that “overflow”?

时光总嘲笑我的痴心妄想 提交于 2019-12-08 15:32:12
问题 In the current C++ Standard Draft, the left shift operator is defined as follows [expr.shift]: The value of E1 << E2 is the unique value congruent to E1×2^E2 modulo 2^N , where N is the width of the type of the result. Consider int E1 = 2^31-1 = 2'147'483'647 , E2 = 1 , and int having 32 bits. Then there is an infinite number of numbers congruent to E1×2^E2 = 4'294'967'294 modulo 2^N = 2^32 , namely, all the numbers 4'294'967'294 + k×2^32 where k is an arbitrary integer. Examples are 4'294

Bitshifting std_logic_vector while keep precision and conversion to signed

≯℡__Kan透↙ 提交于 2019-12-08 09:33:09
问题 In VHDL I want to take a 14 bit input and append '00' on the end to give me a 16 bit number which is the 14 bit input multiplied by 4 and then put this into a 17 bit signed variable such that it is positive (the input is always positive). How should I go about this? like this? shiftedInput <= to_signed('0' & input & '00', 17); Or maybe like this? shiftedInput <= to_signed(input sll 2, 17); Or this? shiftedInput <= to_signed(input & '00', 17); Does it see that the std_logic_vector it's getting