bit-shift

Verilog Barrel Shifter

只愿长相守 提交于 2019-11-28 11:34:04
I want to create a 64-bit barrel shifter in verilog (rotate right for now). I want to know if there is a way to do it without writing a 65 part case statement? Is there a way to write some simple code such as: Y = {S[i - 1:0], S[63:i]}; I tried the code above in Xilinx and get an error: i is not a constant. Main Question: Is there a way to do this without a huge case statment? I've simplified some of the rules for clarity, but here are the details. In the statement Y = {S[i - 1:0], S[63:i]}; you have a concatenation of two signals, each with a constant part select. A constant part select is of

Why doesn't left bit shift << shift beyond 31 for long int datatype?

假如想象 提交于 2019-11-28 10:13:12
I want to use the following code in my program but gcc won't allow me to left shift my 1 beyond 31. sizeof(long int) displays 8, so doesn't that mean I can left shift till 63? #include <iostream> using namespace std; int main(){ long int x; x=(~0 & ~(1<<63)); cout<<x<<endl; return 0; } The compiling outputs the following warning: left shift `count >= width` of type [enabled by default] `x=(~0 & ~(1<<63))`; ^ and the output is -1. Had I left shifted 31 bits I get 2147483647 as expected of int. I am expecting all bits except the MSB to be turned on thus displaying the maximum value the datatype

Why is (-1 >>> 32) = -1? [duplicate]

心不动则不痛 提交于 2019-11-28 10:02:22
Possible Duplicate: why is 1>>32 == 1? -1 as an int converted to binary is represented by 32 1's. When I right-shift it 31 times, I get 1 (31 0's and one 1). But when I right-shift it 32 times, I get -1 again. Shouldn't it be equal to 0? The Java specification explains the shift operators as follows: If the promoted type of the left-hand operand is int , only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & ( §15.22.1 ) with the mask value 0x1f . The shift distance actually

unexpected behavior of bitwise shifting using gcc

僤鯓⒐⒋嵵緔 提交于 2019-11-28 09:18:37
问题 I have a test program like this: int main() { unsigned n = 32; printf("ans << 32 = 0x%X\n", (~0x0U) << 32); printf("ans >> 32 = 0x%X\n", (~0x0U) >> 32); printf("ans << n(32) = 0x%X\n", (~0x0U) << n); printf("ans >> n(32) = 0x%X\n", (~0x0U) >> n); return 0; } It produces the following output: ans << 32 = 0x0 ... (1) ans >> 32 = 0x0 ... (2) ans << n(32) = 0xFFFFFFFF ... (3) ans >> n(32) = 0xFFFFFFFF ... (4) I was expecting (1) and (3) to be the same, as well as (2) and (4) to be the same. Using

What's the reason high-level languages like C#/Java mask the bit shift count operand?

☆樱花仙子☆ 提交于 2019-11-28 09:06:29
This is more of a language design rather than a programming question. The following is an excerpt from JLS 15.19 Shift Operators : If the promoted type of the left-hand operand is int , only the five lowest-order bits of the right-hand operand are used as the shift distance. If the promoted type of the left-hand operand is long , then only the six lowest-order bits of the right-hand operand are used as the shift distance. This behavior is also specified in C# , and while I'm not sure if it's in the official spec for Javascript (if there's one), it's also true based on my own test at least. The

Bitwise operations and shifts

会有一股神秘感。 提交于 2019-11-28 08:39:06
Im having some trouble understanding how and why this code works the way it does. My partner in this assignment finished this part and I cant get ahold of him to find out how and why this works. I've tried a few different things to understand it, but any help would be much appreciated. This code is using 2's complement and a 32-bit representation. /* * fitsBits - return 1 if x can be represented as an * n-bit, two's complement integer. * 1 <= n <= 32 * Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 15 * Rating: 2 */ int fitsBits(int x, int n) { int r,

Signed extension from 24 bit to 32 bit in C++

a 夏天 提交于 2019-11-28 08:20:41
问题 I have 3 unsigned bytes that are coming over the wire separately. [byte1, byte2, byte3] I need to convert these to a signed 32-bit value but I am not quite sure how to handle the sign of the negative values. I thought of copying the bytes to the upper 3 bytes in the int32 and then shifting everything to the right but I read this may have unexpected behavior. Is there an easier way to handle this? The representation is using two's complement. 回答1: You could use: uint32_t sign_extend_24_32

warning: left shift count >= width of type

若如初见. 提交于 2019-11-28 06:41:57
I'm very new to dealing with bits and have got stuck on the following warning when compiling: 7: warning: left shift count >= width of type My line 7 looks like this unsigned long int x = 1 << 32; This would make sense if the size of long on my system was 32 bits. However, sizeof(long) returns 8 and CHAR_BIT is defined as 8 suggesting that long should be 8x8 = 64 bits long. What am I missing here? Are sizeof and CHAR_BIT inaccurate or have I misunderstood something fundamental? long may be a 64-bit type, but 1 is still an int . You need to make 1 a long int using the L suffix: unsigned long x

Bit shifting N bits

限于喜欢 提交于 2019-11-28 06:34:40
问题 Hello quick question regarding bit shifting I have a value in HEX = new byte[] { 0x56, 0xAF }; which is 0101 0110 1010 1111 i want to the first n bits, example 12 then shift off the remaining 4 (16-12) to get 0000 0101 0110 1010 (1386 dec) i cant wrap my head around it and make it scalable for n bits. Thanks! 回答1: Sometime ago i coded these two functions, the first one shifts an byte[] a specified amount of bits to the left, the second does the same to the right: Left Shift: public byte[]

Nested templates vs shift operator

此生再无相见时 提交于 2019-11-28 03:56:04
问题 I have been reading all around about be aware >> as ending of nested template and >> as shift operator... Now I have tried it in my MSVS2010 and no problem occured. std::map<int, std::pair<int, int>> m; This code works exactly what I want (map of pairs) but I supposed to get some error about >> Compiler is smarter these days? 回答1: MSVC++2010 supports C++0x feature Right Angle Brackets 回答2: Be careful because previously good C++03 code may break with compilers supporting this feature. MyArray<