bit-shift

How do I save multiple small integers in one integer via bitshifting?

隐身守侯 提交于 2019-12-06 09:45:48
I am working in an int[][][] array and I need to return an address of one field of that array from a static function. Given the fact that the dimensions of the Array will stay small ( int[32][32][32] ) I had the idea to return one number containing all three Values, instead of using an Array containing the three numbers. I already had a working solution, where i packed my number into a String and unpacked it in the receiving method via Integer.parseInt(String) . Unfortunately this didn't work quite, well in terms of runtime, so i thought of bitshifting. I apologize for my bad english and hope

java : shift distance for int restricted to 31 bits

孤街醉人 提交于 2019-12-06 06:25:07
Any idea why shift distance for int in java is restricted to 31 bits (5 lower bits of the right hand operand)? http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.19 x >>> n I could see a similar question java Bit operations >>> shift but nobody pointed the right answer The shift distance is restricted to 31 bits because a Java int has 32 bits. Shifting an int number by more than 32 bits would produce the same value (either 0 or 0xFFFFFFFF , depending on the initial value and the shift operation you use). It's a design decision, but it seems a bit unfortunate at least for some

How to change 4 bits in unsigned char?

梦想的初衷 提交于 2019-12-06 04:22:48
问题 unsigned char *adata = (unsigned char*)malloc(500*sizeof(unsigned char)); unsigned char *single_char = adata+100; How do I change first four bits in single_char to represent values between 1..10 (int)? The question came from TCP header structure: Data Offset: 4 bits The number of 32 bit words in the TCP Header. This indicates where the data begins. The TCP header (even one including options) is an integral number of 32 bits long. Usually it has value of 4..5, the char value is like 0xA0. 回答1:

difference between JavaScript bit-wise operator code and Python bit-wise operator code

大兔子大兔子 提交于 2019-12-06 03:54:02
I have converted JavaScript code which uses bit-wise operators in that code to Python code, but there is one problem when i do this in JavaScript and Python 412287 << 10 then I get this 422181888 same results in both languages. but when i do this in both 424970184 << 10 then i get different results in both of the languages 1377771520 in JavaScript and 435169468416 in Python can anybody help me with this? any help would be appreciated. If you want the java-script equivalent value then what you can do is : import ctypes print(ctypes.c_int(424970184 << 10 ^ 0).value) Output: 1377771520 Jean

The best way to shift a __m128i?

a 夏天 提交于 2019-12-06 02:40:18
问题 I need to shift a __m128i variable, (say v), by m bits, in such a way that bits move through all of the variable (So, the resulting variable represents v*2^m). What is the best way to do this?! Note that _mm_slli_epi64 shifts v0 and v1 seperately: r0 := v0 << count r1 := v1 << count so the last bits of v0 missed, but I want to move those bits to r1. Edit: I looking for a code, faster than this (m<64): r0 = v0 << m; r1 = v0 >> (64-m); r1 ^= v1 << m; r2 = v1 >> (64-m); 回答1: For compile-time

14-bit left-justified two's complement to a signed short

风格不统一 提交于 2019-12-05 21:39:42
I have two bytes containing a 14-bit left-justified two's complement value, and I need to convert it to a signed short value (ranging from -8192 to +8191, I guess?) What would be the fastest way to do that? Oliver Charlesworth Simply divide by 4. (Note, right-shift leads to implementation/undefined behaviour.) A portable solution: short convert(unsigned char hi, unsigned char lo) { int s = (hi << 6) | (lo >> 2); if (s >= 8192) s -= 16384; return s; } 来源: https://stackoverflow.com/questions/14710764/14-bit-left-justified-twos-complement-to-a-signed-short

Applications of a circular shift

心已入冬 提交于 2019-12-05 21:03:00
I would like to know some examples of application of circular shifts. For example, a right shift on an unsigned integer would result in a division by two. Conversely, a left shift would result in a multiplication by 2. Are there any famous/interesting properties of a circular shift on binary numbers. Note: The example about the right/left shift is to illustrate an application of that particular operator. I am asking for similar examples for the circular shift operator/function. Convert a 16-bit word between big-endian and little-endian representation: right or left circular shift by 8.

Converting from 8 bits to 1 byte

若如初见. 提交于 2019-12-05 13:05:13
I have a string of 8 bits and I want to convert it into 1 byte. I am not sure why my function is not working properly. I have 8 bits stored into an array of 8 unsigned chars. This is my method so far: unsigned int bitsToBytes(unsigned char *bits) { unsigned int sum = 0; for(int i = 7; i >= 0; i--) { sum += bits[i]; sum<<=1; } return sum; } int main() { unsigned char bits[8]; unsigned int byt; byt = bitsToBytes(bits); cout << byt; //doesn't give me the right result } EDIT: My array of bits contains '1' and '0' in the array! Sorry for not being clear. Might anyone know where I went wrong in this

c standard and bitshifts

最后都变了- 提交于 2019-12-05 12:47:39
This question was first inspired by the (unexpected) results of this code: uint16_t t16 = 0; uint8_t t8 = 0x80; uint8_t t8_res; t16 = (t8 << 1); t8_res = (t8 << 1); printf("t16: %x\n", t16); // Expect 0, get 0x100 printf(" t8: %x\n", t8_res); // Expect 0, get 0 But it turns out this makes sense: 6.5.7 Bitwise shift operators Constraints 2 Each of the operands shall have integer type Thus the originally confused line is equivalent to: t16 = (uint16_t) (((int) t8) << 1); A little non-intuitive IMHO, but at least well-defined. Ok, great, but then we do: { uint64_t t64 = 1; t64 <<= 31; printf("t64

PHP Unsigned Right Shift - Malfunctioning

…衆ロ難τιáo~ 提交于 2019-12-05 11:37:07
So, when using my method to preform a ( >>> ) unsigned right shift in PHP, the result is incorrect when the numbers involve negatives. PHP Application Results: INPUT: 10 >>> 3 INPUT: -10 >>> 3 OUTPUT: 1 OUTPUT: 2684354558 JAVA APPLICATION RESULTS: INPUT: 10 >>> 3 INPUT: -10 >>> 3 OUTPUT: 1 OUTPUT: 536870910 (The top results are correct and generated by Java and then the bottom results are incorrect and generated by PHP) It's only when the number is negative in PHP that it fails. The shifts being used in those applications is: Please help if you can! Method for shifting in PHP: function urshift