bit-shift

Arithmetic shift acts as a logical shift, regardless of the signed variable

帅比萌擦擦* 提交于 2019-11-29 10:46:30
I've got a register declared as so: logic signed [15:0][2:0][15:0] registers; When I place a 2's compliment number into the array and arithmetically shift the number, it logical shifts instead: registers[0][0] = 16'b1000000000000000; registers[0][0] = registers[0][0]>>>2; Apparently, the system will logical shift instead of arithmetically shift if the number is not signed. However as you can clearly see, 'registers' is definitely signed. Does anybody know what I might be missing here? Thanks! With Verilog, once you take a part-select, the result is unsigned . Use the $signed system task on the

Is a logical right shift by a power of 2 faster in AVR?

我与影子孤独终老i 提交于 2019-11-29 10:23:42
I would like to know if performing a logical right shift is faster when shifting by a power of 2 For example, is myUnsigned >> 4 any faster than myUnsigned >> 3 I appreciate that everyone's first response will be to tell me that one shouldn't worry about tiny little things like this, it's using correct algorithms and collections to cut orders of magnitude that matters. I fully agree with you, but I am really trying to squeeze all I can out of an embedded chip (an ATMega328) - I just got a performance shift worthy of a 'woohoo!' by replacing a divide with a bit-shift, so I promise you that this

Shifting the sign bit in .NET

心已入冬 提交于 2019-11-29 09:23:14
I'm reading bits from a monochrome bitmap. I'm storing every 16 bits in a short in the reverse order. If the bit in the bitmap is black, store a 1. If white, store a 0. E.g.: for bitmap: bbbw bbbw bbbw wwww my short is: 0000 0111 0111 0111 The 1st way I tried to do this was: short m; // ... Color c = bmp.GetPixel(j, i); if (c.R == Color.Black) m |= short.MinValue; m >>= 1; // ... After one assignment and shift, I got the expected -32768 (1000 0000 0000 0000). After the 2nd time I got -16384 (1100 0000 0000 0000). I changed my code to use ushort and changed the if line to s |= (ushort)Math.Pow

Bit shifting an int 32 times in C

北城以北 提交于 2019-11-29 08:08:45
I have a specific C bit-shifting scenario that I do not believe is covered on Stack Overflow yet. (If it is, I haven't been able to find it!) This exercise is using signed int s as the data type. Take the value 0x80000000 (which is just a 1 in the most significant bit.) Shift it right once on a machine using arithmetic right-shifts (which mine does). Result = 0xC0000000 (1100 0000 in leftmost byte). Continue shifting it and you should be filling up with ones, from the left to the right. Result = 0xFFFFFFFF (All ones.) However: Try the same example but shift one extra position, all together:

Is Shifting more than 32 bits of a uint64_t integer on an x86 machine Undefined Behavior?

梦想的初衷 提交于 2019-11-29 06:05:55
Learning the hard way, I tried to left shift a long long and uint64_t to more than 32 bits on an x86 machine resulted 0 . I vaguely remember to have read somewhere than on a 32 bit machine shift operators only work on the first 32 bits but cannot recollect the source. I would like to know is if Shifting more than 32 bits of a uint64_t integer on an x86 machine is an Undefined Behavior? Daniel Fischer The standard says (6.5.7 in n1570): 3 The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand

Why use only the lower five bits of the shift operand when shifting a 32-bit value? (e.g. (UInt32)1 << 33 == 2)

与世无争的帅哥 提交于 2019-11-29 05:51:24
Consider the following code: UInt32 val = 1; UInt32 shift31 = val << 31; // shift31 == 0x80000000 UInt32 shift32 = val << 32; // shift32 == 0x00000001 UInt32 shift33 = val << 33; // shift33 == 0x00000002 UInt32 shift33a = (UInt32)((UInt64)val << 33); // shift33a == 0x00000000 It doesn't generate a warning (about using a shift greater than 32) so it must be an expected behavior. The code that actually gets put out to the generated assembly (or at least Reflector's interpretation of the code) is uint val = 1; uint shift31 = val << 0x1f; uint shift32 = val; uint shift33 = val << 1; uint shift33a

Difference between SHL and SAL in 80x86

烂漫一生 提交于 2019-11-29 05:28:00
I have learned how to work with 80x86 assembler, so in bit-wise shift operation, I faced a problem with SAL and SHL usage. I means the difference between lines of code as follow : MOV X, 0AAH SAL X, 4 MOV X, 0AAH SHL X, 4 When we should use SHL and when use SAL? What is the difference of them? According to this , they are the same: The shift arithmetic left (SAL) and shift logical left (SHL) instructions perform the same operation; they shift the bits in the destination operand to the left (toward more significant bit locations). For each shift count, the most significant bit of the

Negative logical shift

女生的网名这么多〃 提交于 2019-11-29 02:45:45
In Java, why does -32 >>> -1 = 1 ? It's not specific to just -32. It works for all negative numbers as long as they're not too big. I've found that x >>> -1 = 1 x >>> -2 = 3 x >>> -3 = 7 x >>> -4 = 15 given 0 > x > some large negative number Isn't >>> -1 the same as << 1? But -32 << 1 = -64. I've read up on two's complements, but still don't understand the reasoning. It's because when you are shifting a 32-bit int , it just takes the last 5 bits of the shift distance. (i.e. mod 32), so -1 mod 32 = 31, so you are shifting right by 31 bits. When you are shifting a negative number (the beginning

Is 1 << 31 well defined in C when sizeof(int) == 4

倾然丶 夕夏残阳落幕 提交于 2019-11-29 01:52:10
问题 According to the answer to this questions: The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2 E2 , reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2 E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined. Which seems to imply that 1 << 31 is

Should I bit-shift to divide by 2 in Java? [duplicate]

£可爱£侵袭症+ 提交于 2019-11-28 22:43:09
Possible Duplicates: Is shifting bits faster than multiplying and dividing in Java? .NET? Quick Java Optimization Question Many years ago in college, I learned that bit-shifting right by one accomplishes the same thing as dividing by two, but is generally significantly faster. I'm not sure how Java has come along in that regards since the 9-10 years ago I learned about that. Does the Java compiler automatically converts a divide-by-two into a bit-shift operation, or should I manually perform the bit-shift operation in the code myself? Unless you're working in a shop and a codebase where bit