bit-manipulation

How to bitwise-and CFBitVector

十年热恋 提交于 2019-12-31 06:58:06
问题 I have two instances of CFMutableBitVector , like so: CFBitVectorRef ref1, ref2; How can I do bit-wise operations to these guys? For right now, I only care about and , but obviously xor , or , etc would be useful to know. Obviously I can iterate through the bits in the vector, but that seems silly when I'm working at the bit level. I feel like there are just some Core Foundation functions that I'm missing, but I can't find them. Thanks, Kurt 回答1: Well a CFBitVectorRef is a typedef const

Find most significant set bit in a long

為{幸葍}努か 提交于 2019-12-31 06:41:07
问题 I'm in the unique situation where searching for "most significant bit" yields too many results and I can't find an answer that fits my needs! The question itself is pretty simple: "How do I find the most significant set bit in an unsigned long?" When I do my calculations the rightmost bit position is position '0'. I know that it involves masking the lowest bit, checking and then shifting left to once while incrementing my count, and then repeating with the 2nd lowest, etc. I've done this

Error when trying to perform a bitwise not (~) on a UInt16 in C#

梦想的初衷 提交于 2019-12-31 05:27:08
问题 For some reason, I am simply not understanding (or seeing) why this works: UInt32 a = 0x000000FF; a &= ~(UInt32)0x00000001; but this does not: UInt16 a = 0x00FF; a &= ~(UInt16)0x0001; it gives the error 'constant value -(some number) cannot be converted to a UInt16'. Can someone explain why this is and how to work around the problem? 回答1: The bitwise negation promotes the result to a int , despite the cast. You can overcome this by bitwise and-ing the result of the bitwise negation to the

What is this operator: &=

无人久伴 提交于 2019-12-31 03:26:25
问题 Can someone explain what is the operator &= for? I searched, but I got only results with & or = . 回答1: a &= b; Is the same as a = a & b; & is the "bitwise and operator", search for that. 回答2: It's a shorthand operator which allows you to collapse a = a & b into a &= b Apart from bitwise operations on integers, &= can be used on boolean values as well, allowing you to collapse a = a && b into a &= b However, in the case of logical operation, the expanded form is short-circuiting, while the

efficient way to divide a very large number stored in 2 registers by a constant

情到浓时终转凉″ 提交于 2019-12-31 02:49:12
问题 Let's say I want to calculate the following: A/Z Where A is of length 128 bit and Z is 64 bit long. A is stored in 2 64 bit registers since the registers of the system can store up to 64 bits. What would be an efficient way to calculate the result? P.S: I've solved similar multiplication problems by using CSD representations. However, this would require calculating 1/Z first. 回答1: The right way to solve such a problem, is by returning to the basics: divide the most significant register by the

Bitwise memmove

℡╲_俬逩灬. 提交于 2019-12-30 18:06:07
问题 What is the best way to implement a bitwise memmove ? The method should take an additional destination and source bit-offset and the count should be in bits too. I saw that ARM provides a non-standard _membitmove, which does exactly what I need, but I couldn't find its source. Bind's bitset includes isc_bitstring_copy, but it's not efficient I'm aware that the C standard library doesn't provide such a method, but I also couldn't find any third-party code providing a similar method. 回答1:

Simple bitwise manipulation for little-endian integer, in big-endian machine?

这一生的挚爱 提交于 2019-12-30 10:07:06
问题 For a specific need I am building a four byte integer out of four one byte chars, using nothing too special (on my little endian platform): return (( v1 << 24) | (v2 << 16) | (v3 << 8) | v4); I am aware that an integer stored in a big endian machine would look like AB BC CD DE instead of DE CD BC AB of little endianness, although would it affect the my operation completely in that I will be shifting incorrectly, or will it just cause a correct result that is stored in reverse and needs to be

How many times will the while loop be executed?

半世苍凉 提交于 2019-12-30 09:40:13
问题 I am wondering about how many times this while loop would execute. This is a function that adds two numbers using XOR and AND. def Add(x, y): # Iterate till there is no carry while (y != 0): # carry now contains common # set bits of x and y carry = x & y # Sum of bits of x and y where at # least one of the bits is not set x = x ^ y # Carry is shifted by one so that # adding it to x gives the required sum y = carry << 1 return x `` 回答1: Algorithm for No Carry Adder : function no_carry_adder(A

How to set bits of a bit vector efficiently in parallel?

若如初见. 提交于 2019-12-30 08:12:41
问题 Consider a bit vector of N bits in it ( N is large) and an array of M numbers ( M is moderate, usually much smaller than N ), each in range 0..N-1 indicating which bit of the vector must be set to 1 . The latter array is not sorted. The bit vector is just an array of integers, specifically __m256i , where 256 bits are packed into each __m256i structure. How can this work be split efficiently accross multiple threads? Preferred language is C++ (MSVC++2017 toolset v141), assembly is also great.

How to set bits of a bit vector efficiently in parallel?

做~自己de王妃 提交于 2019-12-30 08:11:17
问题 Consider a bit vector of N bits in it ( N is large) and an array of M numbers ( M is moderate, usually much smaller than N ), each in range 0..N-1 indicating which bit of the vector must be set to 1 . The latter array is not sorted. The bit vector is just an array of integers, specifically __m256i , where 256 bits are packed into each __m256i structure. How can this work be split efficiently accross multiple threads? Preferred language is C++ (MSVC++2017 toolset v141), assembly is also great.