bits

java Bit operations >>> shift

拟墨画扇 提交于 2019-12-01 03:52:58
Why if int x = -1 // binary: 11111111111111111111111111111111 x = x >>> 31; we have 00000000000000000000000000000001 but if int x = -1 x = x >>> 32; we have 11111111111111111111111111111111 (again -1) but not 00000000000000000000000000000000 ? From Section 15.19 of JLS : 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 (0b11111) . The shift distance actually used is therefore

What is the fastest way to calculate the number of bits needed to store a number

若如初见. 提交于 2019-12-01 03:29:17
I'm trying to optimize some bit packing and unpacking routines. In order to do the packing I need to calculate the number of bits needed to store integer values. Here is the current code. if (n == -1) return 32; if (n == 0) return 1; int r = 0; while (n) { ++r; n >>= 1; } return r; You're looking to determine the integer log base 2 of a number (the l=highest bit set). Sean Anderson's "Bit Twiddling Hacks" page has several methods ranging from the obvious counting bits in a loop to versions that use table lookup. Note that most of the methods demonstrated will need to be modified a bit to work

java Bit operations >>> shift

北城余情 提交于 2019-12-01 00:38:06
问题 Why if int x = -1 // binary: 11111111111111111111111111111111 x = x >>> 31; we have 00000000000000000000000000000001 but if int x = -1 x = x >>> 32; we have 11111111111111111111111111111111 (again -1) but not 00000000000000000000000000000000 ? 回答1: From Section 15.19 of JLS: 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

Practical applications of bit shifting

蓝咒 提交于 2019-11-30 16:00:42
问题 I totally understand how to shift bits. I've worked through numerous examples on paper and in code and don't need any help there. I'm trying to come up with some real world examples of how bit shifting is used. Here are some examples I've been able to come up with: Perhaps the most important example I could conceptualize had to do with endianness. In big endian systems, least significant bits are stored from the left, and in little endian systems, least significant bits are stored from the

Practical applications of bit shifting

落花浮王杯 提交于 2019-11-30 15:54:44
I totally understand how to shift bits. I've worked through numerous examples on paper and in code and don't need any help there. I'm trying to come up with some real world examples of how bit shifting is used. Here are some examples I've been able to come up with: Perhaps the most important example I could conceptualize had to do with endianness. In big endian systems, least significant bits are stored from the left, and in little endian systems, least significant bits are stored from the right. I imagine that for files and networking transmissions between systems which use opposite endian

how to do two complement multiplication and division of integers?

放肆的年华 提交于 2019-11-30 12:07:04
问题 I have read this post on binary multiplication using two complement. but it is not very clear to me. Even I have difficulty understanding the wiki article on this. I want to know how to go about calculating multiplications of negative numbers: eg: -1 with -7 should give 7. A 4-bit, 2's complement of -1 is : 1111 A 4-bit, 2's complement of -7 is : 1001 some step-wise way of calculating the multiplication will be helpful. No article I came across talks about division. How to approach this? 回答1:

What are the INFINITY constants in Java, really?

六月ゝ 毕业季﹏ 提交于 2019-11-30 11:47:22
问题 I just recently ran across the constants in the primitive type wrapper classes like Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY . In the API, it defines the first as: A constant holding the positive infinity of type double. It is equal to the value returned by Double.longBitsToDouble(0x7ff0000000000000L). The others have definitions along these same lines. What I'm having trouble with is understanding what these constants actually are. They can't actually be or represent positive

Convert integer to bits

家住魔仙堡 提交于 2019-11-30 07:31:16
I have byte to binary string function, std::string byte_to_binary(unsigned char byte) { int x = 128; std::ostringstream oss; oss << ((byte & 255) != 0); for (int i = 0; i < 7; i++, x/=2) oss << ((byte & x) != 0); return oss.str(); } How can i write an int to bits in same way? I don't want extra 0's at the beginning of binary string so that is why i can't figure out how to create a variable length each time. Also, i'm not using std::bitset. I'll just post this as an answer. It is shorter, safer and, what's most important, it is done . #include <string> #include <bitset> #include <type_traits> /

Get an array of the bit positions within a 64-bit integer

巧了我就是萌 提交于 2019-11-30 06:53:30
OK, it may sound a bit complicated, but this is what I'm trying to do : Take e.g. 10101010101 And return { 0, 2, 4, 6, 8, 10 } - an array with all of the positions of bits which are set This is my code : UINT DQBitboard::firstBit(U64 bitboard) { static const int index64[64] = { 63, 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3, 61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4, 62, 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21, 56, 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5 }; static const U64 debruijn64 = 0x07EDD5E59A4E28C2ULL; #pragma warning

how to do two complement multiplication and division of integers?

别说谁变了你拦得住时间么 提交于 2019-11-30 03:45:06
I have read this post on binary multiplication using two complement. but it is not very clear to me. Even I have difficulty understanding the wiki article on this. I want to know how to go about calculating multiplications of negative numbers: eg: -1 with -7 should give 7. A 4-bit, 2's complement of -1 is : 1111 A 4-bit, 2's complement of -7 is : 1001 some step-wise way of calculating the multiplication will be helpful. No article I came across talks about division. How to approach this? step 1: sign extend both integers to twice as many bits. This is safe to do, though may not always be