bit-shift

What is an intuitive way to interpret the bitwise operators and masking? Also, what is masking used for?

蹲街弑〆低调 提交于 2019-12-23 02:07:06
问题 I'm learning about bitwise operators and masking right now in my computer systems class. However I'm having some trouble internalizing them. I understand what the operators, &, |, ^, >> (both arithmetic and logical shift), and << DO, but I don't quite get what they're really used for aside from optimizing multiplication and division operations (for >> and <<), and to check if certain bits are on or off (the & operator). Also, I don't understand what masking is used for. I know that doing x &

Bit shifting left

心已入冬 提交于 2019-12-23 01:35:12
问题 Let's say I want to bit shift i twice to the left and store the value in f . f = i << 2; Is that correct? How exactly do I do this in C/C++? 回答1: Yes. f = i << 2 Shifts are useful in a number of bit twiddling operations. This used to be a great way to multiply a number by four. However, these days, optimizing compilers tend to take care of that for you. Keep in mind that the two leftmost bits are discarded. 回答2: As an additional note: Even though your question is tagged C++ , it is probably

java : shift distance for int restricted to 31 bits

一世执手 提交于 2019-12-22 14:29:10
问题 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 回答1: 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

Bit shifting in Ruby

瘦欲@ 提交于 2019-12-22 05:27:11
问题 I'm currently converting a Visual Basic application to Ruby because we're moving it to the web. However when converting some algorithms I've run into a problem concerning bit shifting. How I understand it, the problem lies in the size mask VB enforces on Integer types (as explained Here). Ruby, in practice, doesn't differentiate in these types. So the problem: Visual Basic Dim i As Integer = 182 WriteLine(i << 24) '-1241513984 Ruby puts 182 << 24 # 3053453312 I've been Googling and reading up

Manipulating 80 bits datatype in C

Deadly 提交于 2019-12-22 04:38:11
问题 I'm implementing some cryptographic algorithm in C which involves an 80 bits key. A particular operation involves a rotate shifting the key x number of bits. I've tried the long double type which if I'm not wrong is 80bits, but that doesn't work with the bitshift operator. The only alternative I can come up with is to use a 10 element char array with some complicated looping and if-else. My question is whether there's some simple and efficient way of carrying this out. Thanks. 回答1: There is

Java Converting long to bytes - which approach is more efficient

为君一笑 提交于 2019-12-21 20:39:50
问题 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? 回答1: 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]

Get 30th bit of the lParam param in WM_KEYDOWN message

て烟熏妆下的殇ゞ 提交于 2019-12-21 20:37:35
问题 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 回答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

Why do we need to use shift operators in java?

这一生的挚爱 提交于 2019-12-21 03:33:56
问题 What is the purpose of using Shift operators rather than using division and multiplication? Are there any other benefits of using shift operators? Where should one try to use the shift operator? 回答1: Division and multiplication are not really a use of bit-shift operators. They're an outdated 'optimization' some like to apply. They are bit operations, and completely necessary when working at the level of bits within an integer value. For example, say I have two bytes that are the high-order

What is the fastest way to get the 4 least significant bits in a byte (C++)?

丶灬走出姿态 提交于 2019-12-21 02:17:11
问题 I'm talking about this: If we have the letter 'A' which is 77 in decimal and 4D in Hex. I am looking for the fastest way to get D. I thought about two ways: Given x is a byte. x << 4; x >> 4 x %= 16 Any other ways? Which one is faster? 回答1: I always use x &= 0x0f 回答2: Brevity is nice - explanations are better :) x &= 0x0f is, of course, the right answer. It exactly expresses the intent of what you're trying to achieve, and on any sane architecture will always compile down to the minimum

How can I turn an int into three bytes in Java?

一世执手 提交于 2019-12-20 11:35:32
问题 I am trying to convert an int into three bytes representing that int (big endian). I'm sure it has something to do with bit-wise and and bit shifting. But I have no idea how to go about doing it. For example: int myInt; // some code byte b1, b2 , b3; // b1 is most significant, then b2 then b3. *Note, I am aware that an int is 4 bytes and the three bytes have a chance of over/underflowing. 回答1: To get the least significant byte: b3 = myInt & 0xFF; The 2nd least significant byte: b2 = (myInt >>