bit-shift

Get 30th bit of the lParam param in WM_KEYDOWN message

浪尽此生 提交于 2019-12-04 12:29:04
I need to get the 30th bit of the lParam param passed with the WM_KEYDOWN message. This bit as written here allows me to know if the key was pressed before. Is this code right to get it? (lParam >> 30) & 1 I would just use lParam & 0x40000000 . If that's non-zero, then b30 was set (I consider that the thirty first bit of the thirty two, by the way). And there's more likelihood that it will be a {logical-and, compare} operation rather than {shift, logical-and, compare} . Mind you, there's a good chance that a decent compiler would generate the more efficient code anyway even if you used (lParam

Java Converting long to bytes - which approach is more efficient

时光总嘲笑我的痴心妄想 提交于 2019-12-04 12:12:18
I have two approaches to convert long to byte array. for (int i = 0; i < 7; i++) { data[pos + i] = (byte) (value >> (7- i - 1 << 3)); } and for (int i = 7; i >= 0; --i) { data[p + i] = (byte)(newl & 0xff); newl >>= 8; } which of the two operations is more efficient? I suggest you look at how the Java code does it. public final void writeLong(long v) throws IOException { writeBuffer[0] = (byte)(v >>> 56); writeBuffer[1] = (byte)(v >>> 48); writeBuffer[2] = (byte)(v >>> 40); writeBuffer[3] = (byte)(v >>> 32); writeBuffer[4] = (byte)(v >>> 24); writeBuffer[5] = (byte)(v >>> 16); writeBuffer[6] =

Java results differ for (int)Math.pow(2,x) and 1<<x

╄→гoц情女王★ 提交于 2019-12-04 10:55:02
问题 Why do the following two operations yield different results in Java for x = 31 or 32 but the same results for x=3 ? int x=3; int b = (int) Math.pow(2,x); int c = 1<<x; Results: x=32: b=2147483647; c=1; x=31: b=2147483647; c=-2147483648; x=3: b=8 ; c=8 回答1: There are multiple issues at play: An int can only store values between -2147483648 and 2147483647 . 1 << x only uses the lowest five bits of x. Thus, 1 << 32 is by definition the same as 1 << 0 . Shift operations are performed on the two's

.Net GetHashcode Bit Shifting Operation

和自甴很熟 提交于 2019-12-04 10:26:16
问题 I was looking through some of the .net source yesterday and saw several implementations of GetHashcode with something along the lines of this: (i1 << 5) + i ^ i2 I understand what the code is doing and why. What I want to know is why they used (i1 << 5) + i instead of (i1 << 5) - i. Most frameworks I've seen use -i because that's equivalent to multiplying by 31 which is prime, but the Microsoft way is equivalent to multiplying by 33 which has 11 and 3 as factors and thus isn't prime. Is there

What does a >> mean in Go language?

末鹿安然 提交于 2019-12-04 09:14:13
问题 I’m looking for information on Google’s Go language. In “A Tour of Go” they have this code: const ( Big = 1<<100 Small = Big>>99 ) But what do << and >> mean? You can see all of the code at http://tour.golang.org/#14 回答1: They are bitwise shift operators. x << y means x × 2 y , while x >> y means x × 2 −y or, equivalently, x ÷ 2 y . These operators are generally used to manipulate the binary representation of a value, where, just like with a power of 10 in decimal, multiplying or dividing by

The best way to shift a __m128i?

柔情痞子 提交于 2019-12-04 08:39:38
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); For compile-time constant shift counts, you can get fairly good results. Otherwise not really. This is just an SSE

How does this print “hello world”?

岁酱吖の 提交于 2019-12-04 07:21:26
问题 I discovered this oddity: for (long l = 4946144450195624l; l > 0; l >>= 5) System.out.print((char) (((l & 31 | 64) % 95) + 32)); Output: hello world How does this work? 回答1: The number 4946144450195624 fits 64 bits, its binary representation is: 10001100100100111110111111110111101100011000010101000 The program decodes a character for every 5-bits group, from right to left 00100|01100|10010|01111|10111|11111|01111|01100|01100|00101|01000 d | l | r | o | w | | o | l | l | e | h 5-bit

Python Bitshift 32 Bit Constraint [duplicate]

跟風遠走 提交于 2019-12-04 06:17:32
问题 This question already has an answer here : Closed 8 years ago . Possible Duplicate: Problem in calculating checksum : casting int to signed int32 This should be a relatively easy answer, I just don't really know how to search for it...I got a few semi-relevant things, but nothing that fits what I'm trying to do. >>> 1171855803 << 7 149997542784L # I want -326312576 In other words, treat the number as an integer and don't allow it to convert to a long. How would I do this? I tried the solution

How to bitwise shift in VB.NET?

Deadly 提交于 2019-12-04 03:01:47
问题 How do I bitwise shift right/left in VB.NET? Does it even have operators for this, or do I have to use some utility method? 回答1: VB.NET has had bit shift operators (<< and >>) since 2003. 回答2: You can use the << and >> operators, and you have to specify how many bits to shift. myFinal = myInteger << 4 ' Shift LEFT by 4 bits. myFinal = myInteger >> 4 ' Shift RIGHT by 4 bits. You can also use it as a unary operator... myFinal <<= 4 ' Shift myFinal LEFT by 4 bits, storing the result in myFinal.

MIPS Using Bit Shift Operators to Print a Decimal in Binary

走远了吗. 提交于 2019-12-04 02:30:42
问题 I've read numerous threads here and elsewhere online concerning this topic. Great topics regarding bit shifts (not necessarily pertaining to Assembly but the topic in general are: What are bitwise shift (bit-shift) operators and how do they work? I've gone as far as copying and pasting the code from the OP here: How do I print a binary number with an inputed integer? and making the changes that the replier had suggested and I continue to produce a string of zero's no matter what I do. I