bit-shift

Java - bit shifting with integers and bytes

∥☆過路亽.° 提交于 2019-12-01 01:35:57
Consider the following code (where byteIndex is an int): int bitNumber = b-(8*byteIndex); bitMask = 0x8>>(byte)bitNumber; This generates the error error: possible loss of precision when compiled (required byte, found int). The code int bitNumber = b-(8*byteIndex); bitMask = 0x8>>2; compiles fine. What is the problem here and how do I fix the first example to allow bit shifting by the int value? EDIT: Following the comments, here is a more-complete example: 48) int byteIndex; 49) byte bitMask; 50) int bitNumber; // assign value to byteIndex 67) bitNumber = b-(8*byteIndex); 68) bitMask = 0x8>

What does shift left (<<) actually do in Swift?

我的未来我决定 提交于 2019-12-01 01:11:59
问题 I'm messing around with a Flappy Bird clone, and i can't figure out what the meaning of the following code is let birdCategory: UInt32 = 1 << 0 let worldCategory: UInt32 = 1 << 1 let pipeCategory: UInt32 = 1 << 2 let scoreCategory: UInt32 = 1 << 3 Sorry if this is obvious, i've tried looking for the answer but couldn't find it. Thanks 回答1: That's the bitwise left shift operator Basically it's doing this // moves 0 bits to left for 00000001 let birdCategory: UInt32 = 1 << 0 // moves 1 bits to

Getting upper and lower byte of an integer in C# and putting it as a char array to send to a com port, how?

北战南征 提交于 2019-11-30 23:53:29
问题 In C I would do this int number = 3510; char upper = number >> 8; char lower = number && 8; SendByte(upper); SendByte(lower); Where upper and lower would both = 54 In C# I am doing this: int number = Convert.ToInt16("3510"); byte upper = byte(number >> 8); byte lower = byte(number & 8); char upperc = Convert.ToChar(upper); char lowerc = Convert.ToChar(lower); data = "GETDM" + upperc + lowerc; comport.Write(data); However in the debugger number = 3510, upper = 13 and lower = 0 this makes no

How are shifts implemented on the hardware level?

我与影子孤独终老i 提交于 2019-11-30 22:48:01
问题 How are bit shifts implemented at the hardware level when the number to shift by is unknown? I can't imagine that there would be a separate circuit for each number you can shift by (that would 64 shift circuits on a 64-bit machine), nor can I imagine that it would be a loop of shifts by one (that would take up to 64 shift cycles on a 64-bit machine). Is it some sort of compromise between the two or is there some clever trick? 回答1: The circuit is called a "barrel shifter" - it's a load of

Using logical bitshift for RGB values

删除回忆录丶 提交于 2019-11-30 22:11:21
I'm a bit naive when it comes to bitwise logic and I have what is probably a simple question... basically if I have this (is ActionScript but can apply in many languages): var color:uint = myObject.color; var red:uint = color >>> 16; var green:uint = color >>> 8 & 0xFF; var blue:uint = color & 0xFF; I was wondering what exactly the `& 0xFF' is doing to green and blue. I understand what an AND operation does, but why is it needed (or a good idea) here? The source for this code was here: http://alexgblog.com/?p=680 Appreciate the tips. Alex In RGB you have 8 bits for Red, 8 bits for Green and 8

SIMD versions of SHLD/SHRD instructions

你离开我真会死。 提交于 2019-11-30 20:44:18
SHLD/SHRD instructions are assembly instructions to implement multiprecisions shifts. Consider the following problem: uint64_t array[4] = {/*something*/}; left_shift(array, 172); right_shift(array, 172); What is the most efficient way to implement left_shift and right_shift , two functions that operates a shift on an array of four 64-bit unsigned integer as if it was a big 256 bits unsigned integer? Is the most efficient way of doing that is by using SHLD/SHRD instructions, or is there better (like SIMD versions) instructions on modern architecture? In this answer I'm only going to talk about

K&R - Understanding exercise 2-8: Exactly what is asked here?

不羁岁月 提交于 2019-11-30 20:27:08
I'm working through the exercises in the K&R book. Currently I'm stuck at exercise 2-8, which is says the following: Write a function rightrot(x, n) that returns the value of the integer x rotated to the right by n bit positions. The trouble I have is that I cannot seem to picture what the result SHOULD look like. How or what do I rotate? Do I take the leftmost bit and put it to the rightmost position of x , after x is shifted to the left and repeat this for n bits? Or do I take a chunk ( n bits) and put it n bits to the right while leaving the rest of the rightmost bits unchanged? Any helpful

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

bit shift operation does not return expected result

十年热恋 提交于 2019-11-30 14:11:22
问题 Why does Java return -2147483648 when I bit shift 1 << 63 ? The expected result is 9 223 372 036 854 775 808 , tested with Wolfram Alpha and my calculator. I tested: System.out.print((long)(1 << (63))); 回答1: There's an important thing to note about the line System.out.print((long)(1 << (63))); You first take (1 << 63) , and then you cast to long. As a result, you are actually left-shifting in integers, so the long cast doesn't have any effect. That's why shifting 63 bits left gives the min