bit-manipulation

How to get the bits of a “double” as a “long”

天大地大妈咪最大 提交于 2019-12-10 01:52:42
问题 I would like to manipulate the bitwise representation of floating-point numbers in C#. BinaryWriter and BinaryReader do it this way: public virtual unsafe void Write(double value) { ulong num = *((ulong*) &value); ... } public virtual unsafe double ReadDouble() { ... ulong num3 = ...; return *((double*) &num3); } Is there a way to do this without unsafe code, and without the overhead of actually using BinaryWriter and BinaryReader? 回答1: Are you trying to avoid unsafe code altogether, or do

Rounded division by power of 2

独自空忆成欢 提交于 2019-12-10 01:16:08
问题 I'm implementing a quantization algorithm from a textbook. I'm at a point where things pretty much work, except I get off-by-one errors when rounding. This is what the textbook has to say about that: Rounded division by 2^p may be carried out by adding an offset and right-shifting by p bit positions Now, I get the bit about the right shift, but what offset are they talking about? Here's my sample code: def scale(x, power2=16): if x < 0: return -((-x) >> power2) else: return x >> power2 def

Converting rgba values into one integer in Javascript

僤鯓⒐⒋嵵緔 提交于 2019-12-10 01:04:28
问题 I can already convert 32bit integers into their rgba values like this: pixelData[i] = { red: pixelValue >> 24 & 0xFF, green: pixelValue >> 16 & 0xFF, blue: pixelValue >> 8 & 0xFF, alpha: pixelValue & 0xFF }; But I don't really know how to reverse it. 回答1: To reverse it, you just have to combine the bytes into an integer. Simply use left-shift and add them, and it will work. var rgb = (red << 24) + (green << 16) + (blue << 8) + (alpha); Alternatively, to make it safer, you could first AND each

C++: Bitwise AND

痞子三分冷 提交于 2019-12-10 00:28:28
问题 I am trying to understand how to use Bitwise AND to extract the values of individual bytes. What I have is a 4-byte array and am casting the last 2 bytes into a single 2 byte value. Then I am trying to extract the original single byte values from that 2 byte value. See the attachment for a screen shot of my code and values. The problem I am having is I am not able to get the value of the last byte in the 2 byte value. How would I go about doing this with Bitwise AND? Thanks for your help,

c: bit reversal logic

99封情书 提交于 2019-12-09 23:59:29
问题 I was looking at the below bit reversal code and just wondering how does one come up with these kind of things. (source : http://www.cl.cam.ac.uk/~am21/hakmemc.html) /* reverse 8 bits (Schroeppel) */ unsigned reverse_8bits(unsigned41 a) { return ((a * 0x000202020202) /* 5 copies in 40 bits */ & 0x010884422010) /* where bits coincide with reverse repeated base 2^10 */ /* PDP-10: 041(6 bits):020420420020(35 bits) */ % 1023; /* casting out 2^10 - 1's */ } Can someone explain what does comment

How to swap nybbles in C?

匆匆过客 提交于 2019-12-09 21:10:06
问题 How to swap the nybble bit positions of a number? For example: 534, convert it into binary, the rightmost 4 bits has to be interchanged with the leftmost 4 bits and then make a new number with that. Anyone know how to do this? 回答1: Start from the fact that hexadecimal 0xf covers exactly four bits. There are four nibbles in a 16-bit number. The masks for the nibbles are 0xf000 , 0xf00 , 0xf0 , and 0xf . Then start masking, shifting and bitwise OR-ing. 回答2: Sean Anderson's bit twiddling guide

Binary matrix multiplication bit twiddling hack

江枫思渺然 提交于 2019-12-09 17:35:08
问题 Abstract Hi, suppose you have two different independent 64-bit binary matrices A and T ( T is a transposed version of itself, using the transposed version of matrix allows during multiplication to operate on T 's rows rather than columns which is super cool for binary arithmetic) and you want to multiply these matrices the only thing is that matrix multiplication result is truncated to 64-bits and if you yield to a value greater that 1 in some specific matrix cell the resulting matrix cell

How to find all the subarrays with xor 0?

元气小坏坏 提交于 2019-12-09 14:10:31
问题 The problem is to find all the subarrays of the given array with xor of all its elements equal to zero. For example, if array contains elements [13,8,5,3,3] , the solution should give the indices of all subarrays like 0-2 , 3-4 , 0-4 , etc. The question is similar to the one asked here The only difference is that I want the indices of all the subarrays that satisfies the equation A0 xor A1 xor...xor An = 0 回答1: This is a fairly straightforward extension of the linked question. In Python, #

How to copy bits from one variable to another?

空扰寡人 提交于 2019-12-09 13:07:30
问题 Let's say I have this int variable v1 : 1100 1010 And this variable int v2 : 1001 1110 I need to copy the last four bits from v2 to the last four bits of v1 so that the result is: 1100 1110 ^ ^ last four bits of v2 | | first four bits of v1 How would I got about doing this in C or C++? I read a few articles about bitwise operations but I couldn't find any information specifically about this. 回答1: Bitwise operations were the right things to look for. v1 = (v1 & ~0xf) | (v2 & 0xf); Is there

K&R C Exercise Help

寵の児 提交于 2019-12-09 12:36:50
问题 I've been going through the K&R C Programming Language book and I'm stuck on Exercise 2-6 which reads: Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged. I'm having trouble understanding the exact thing they're looking for me to do. I looked at a possible answer here, but I still don't really understand. I think it's the wording that's throwing me off. Can anyone maybe explain what they