bit-shift

ctypes reimplementation of rshift for c_ulong

余生颓废 提交于 2019-12-19 09:17:59
问题 i am accessing a C library via ctypes and i am stuck with the following problem: I am generating a "wrapper" (ctypes commands to access the library with ctypes) using ctypeslib. The C library contains macros which are converted to python functions in this step. (To be independent of the libraries internals as much as possible i want to use some of these macros in python.) One of these macros looks like this: # using the ctypes types myuint16_t = c_ushort myuint32_t = c_ulong def mymacro(x):

sra(shift right arithmetic) vs srl (shift right logical)

柔情痞子 提交于 2019-12-19 08:29:18
问题 Please take a look at these two pieces of pseudo-assembly code: 1) li $t0,53 sll $t1,$t0,2 srl $t2,$t0,2 sra $t3,$t0,2 print $t1 print $t2 print $t3 2) li $t0,-53 sll $t1,$t0,2 srl $t2,$t0,2 sra $t3,$t0,2 print $t1 print $t2 print $t3 in the first case the output is: 212 13 13 in the latter is: -212 107374... -14 But shouldn't : sra (-53) = - (srl 53) ? 回答1: -53 = 1111111111001011 sra 2 1111111111110010(11) = -14 ^^ ^^ sign dropped extension Because the extra bits are simply dropped for both

LC3 Assembly Bitwise Right Shift

前提是你 提交于 2019-12-19 06:56:33
问题 What I need to do it implement both a bitwise left shift, and a bitwise right shift using LC-3 Assembly. Basically, every bit has to be moved over one space in the direction of the shift, and a zero fills the empty space created. Examples: Right Shift: 01001001 00100100→ Left Shift: 01001001 ←10010010 I've successfully implemented a left shift, by taking the binary string, and adding it to itself. I'm stumped on how to perform a right shift. Any thoughts would be greatly appreciated. I have

Left shift of negative values by 0 positions?

五迷三道 提交于 2019-12-19 05:58:32
问题 In C, a left shift of a negative value is undefined behavior. I've encountered two libraries compiled with Intel's ICC where the offending code was removed. The same code was fine under Clang, Comeau, GCC and MSVC. Does the standard make any mention of left shifting a negative value 0 places? Is it also undefined? (The detail I'm curious about is a 0-sized shift, which is no shift at all in practice. So I'm wondering if the language is vague such that a 0-sized left shift may be allowed). 回答1

Java - bit shifting with integers and bytes

烈酒焚心 提交于 2019-12-19 04:42:21
问题 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;

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

别说谁变了你拦得住时间么 提交于 2019-12-19 03:17:51
问题 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

SIMD versions of SHLD/SHRD instructions

白昼怎懂夜的黑 提交于 2019-12-19 02:51:29
问题 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

circular left shift of an array by n positions in java

大城市里の小女人 提交于 2019-12-18 13:26:23
问题 I am trying to do the circular left shift of an array by n positions using only a single 1D array. I can do it in two arrays, but I haven't figured out how to do it using one. Please give your suggestions 回答1: There is actually a clever algorithm for that. We'll use A to denote the array, N to denote the array size, and n to denote the number of positions to shift. After the shift you would like the i-th element to move to the ((i + n) mode N)-th position, Hence we can define the new

Bit Twiddling Hacks: interleave bits the obvious way [closed]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 11:48:36
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 9 years ago . i am interested on this problem Interleave bits the obvious way (from http://graphics.stanford.edu/~seander/bithacks.html) unsigned short x; // Interleave bits of x and y, so that all of the unsigned short y; //

Perform logical shift using arithmetic shift operator in C [duplicate]

扶醉桌前 提交于 2019-12-18 09:45:39
问题 This question already has answers here : Implementing Logical Right Shift in C (8 answers) Closed 11 months ago . Right now I am reading the book Computer Systems : Programmer Perspective. One problem in the book says to perform a logical right shift on a signed integer, I can't figure out how to start on this. The following is the actual question from the book: Fill in code for the following C functions. Function srl performs a logical right shift using an arithmetic right shift (given by