bit-manipulation

How to add a code fix for infinite loop while adding two integers using bitwise operations

孤街醉人 提交于 2020-01-05 06:09:05
问题 Here is the original question. Here is the code for adding two integers using bitwise operations: def getSum(self, a, b): while (a & b): x = a & b y = a ^ b a = x << 1 b = y return a ^ b Though I know the reason why it goes into an infinite loop while adding a positive and negative integer, I need some help with coming up with the code-fix for this. 回答1: I am assuming that you have gone through the logic behind the infinite loop you are getting. logic Following is the behaviour of your

C++ Fast and Efficient way to perform bit_count and AND operation on 40 byte array

不打扰是莪最后的温柔 提交于 2020-01-05 05:44:08
问题 In my project I need to AND two binary array of size 40 bytes(320 bits) and then compute set bit count in C++. I found some algorithms to do this but I want to know what is the fastest way of implementing it in c++. I mean what c++ data type would be proper?(unsinged char*,unsigned int 32,u_int64,...). I know many algorithms are compatible with 32bit integer although my array size is 40 bytes . what about the algorithms described in this link: Fast Bit Counting Techniques which one is faster?

32 Bit Integer Mask

瘦欲@ 提交于 2020-01-05 02:31:16
问题 I'm finishing up some CSE homework and I have a quick question about declaring integers of larger bit sizes. My task is to implement a function that returns 1 if any odd bit of x is 1 (assuming size of x is 32 bits) and returns 0 otherwise. Am I allowed to declare an integer with the bit value: 10101010101010101010101010101010 If so, are there any problems that could arise from this? If not, why not?? What alternatives do I have? My function: int any_odd_one(unsigned x) { int mask =

Do operations that contain bit-wise AND (&) or bit-wise exclusive OR(^) have a decimal equivalent?

你说的曾经没有我的故事 提交于 2020-01-04 20:46:09
问题 Consider the following expressions in which both operand are decimal : a^b or a&b I know what the operators do on binary digits of operands and hence i know how the answer of a^b or a&b is calculated. What I do not know is if those operations can be translated to decimal form for example : we can say that a<<b is equivalent to this operation : a*pow(2,n) Is a^b or a&b equivalent to anything like that?? 回答1: Bit operations (AND, OR, NOT, XOR) are logical extensions of Boolean algebra. Boolean

Rotating (by 90°) a bit matrix (up to 8x8 bits) within a 64-bit integer

帅比萌擦擦* 提交于 2020-01-04 15:15:10
问题 I have a bit matrix (of size 6x6, or 7x7, or 8x8) stored within one single 64-bit integer. I am looking for c++ code that rotates these matrices by 90, 180, 270 degrees, as well as c++ code for shifting (horizontally and vertically) and mirroring these matrices. The output must be again a 64-bit integer. Using some of the advanced CPU instruction sets would probably be okay, as well as using hash tables or similar techniques - speed is of highest importance, and RAM is available. I will run

Bit shifts with ABAP

风流意气都作罢 提交于 2020-01-04 14:17:37
问题 I'm trying to port some Java code, which requires arithmetic and logical bit shifts, to ABAP. As far as I know, ABAP only supports the bitwise NOT, AND, OR and XOR operations. Does anyone know another way to implement these kind of shifts with ABAP? Is there perhaps a way to get the same result as the shifts, by using just the NOT, AND, OR and XOR operations? 回答1: Disclaimer: I am not specifically familiar with ABAP, hence this answer is given on a more general level. Assuming that what you

python3: How to get logical complement (negation) of a binary number, eg. '010' => '101'?

时光总嘲笑我的痴心妄想 提交于 2020-01-04 06:05:40
问题 Maybe I'm missing something but I can't find a straightforward way to accomplish this simple task. When I go to negate a binary number through the "~" operator it returns a negative number due to the two's complement: >>> bin(~0b100010) # this won't return '0b011101' '-0b100011' What about if I just want to switch 0s into 1s and vice-versa, like in classic logical complement? 回答1: >>> bin(0b111111 ^ 0b100010) '0b11101' >>> 回答2: YOU's answer as a function: def complement(n): size = len(format

Most efficient way to set n consecutive bits to 1?

我是研究僧i 提交于 2020-01-04 04:14:27
问题 I want to get a function that will set the n last bits of a numerical type to 1 . For example: bitmask (5) = 0b11111 = 31 bitmask (0) = 0 I, first, had this implementation ( mask_t is just a typedef around uint64_t ): mask_t bitmask (unsigned short n) { return ((((mask_t) 1) << n) - 1; } Everything is fine except when the function hit bitmask (64) (the size of mask_t ), then I get bitmask (64) = 0 in place of 64 bits set to 1 . So, I have two questions: Why do I have this behavior ? Pushing

Double bitwise NOT (~~) in C#

半城伤御伤魂 提交于 2020-01-04 02:03:12
问题 I've been reading through ThreadLocal<T> implementation and don't quite understand the rationale behind inverting an int id before storing it in a private int filed ( m_idComplement ) and then inverting it back again in almost every expression it's used in. Except for this case applicable to JavaScript, I can't find any information on why would double negation be useful in C#? On line 240 it does assign 0 without negation, but it could've just assigned a -1 and just drop all the other

Double bitwise NOT (~~) in C#

戏子无情 提交于 2020-01-04 02:03:06
问题 I've been reading through ThreadLocal<T> implementation and don't quite understand the rationale behind inverting an int id before storing it in a private int filed ( m_idComplement ) and then inverting it back again in almost every expression it's used in. Except for this case applicable to JavaScript, I can't find any information on why would double negation be useful in C#? On line 240 it does assign 0 without negation, but it could've just assigned a -1 and just drop all the other