  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # How to add a code fix for infinite loop while adding two integers using bitwise operations  » python » How to add a code fix for infinite loop while adding two integers using bitwise operations

By : Minu.Kim
Date : November 21 2020, 11:01 PM
fixed the issue. Will look into that further I am assuming that you have gone through the logic behind the infinite loop you are getting. logic
Following is the behaviour of your program : code :
``````1. Gives correct result when both a and b are positive.

2. Gives correct result when both a and b are negative.

3. Gives correct result when one of them is positive and one of them is negative
and positive one is smaller than negative one.

4. Gives incorrect result (infinite loop) when one of them is positive and one of
them is negative and positive one is bigger than negative one.`
``````
``````# I will leave neagte(x) on you to implement.
# So assume we have  negate() method available.

def getSum(a,b):
# if a is -ve and b is +ve and abs(a) is less than b.
cond1 =  a < 0 and b > 0 and abs(a) < b

# if b is -ve and a is +ve and abs(b) is less than a.
cond2 =  b < 0 and a > 0 and abs(b) < a

# if any of these conditions are met. negate the value of a and b
# and negate the value of answer as well.
if cond1 or cond2:
return negate(_getSum(negate(a),negate(b)))

# otherwise run normally.
return _getSum(a,b)

def _getSum(a, b):
while (a):
x = a & b
y = a ^ b
a = x << 1
b = y
return b
`````` ## Bitwise operations with big integers

By : user3348845
Date : March 29 2020, 07:55 AM
Hope that helps 17516032 in binary is 00000001000010110100011000000000. Shifting to the left by 7 gives you 10000101101000110000000000000000. This is equal to -2052915200 in two's complement (which is how almost all computers represent negative numbers).
>> is a signed right shift. That means that the leftmost bit (which determines the sign of a number) will be shifted into the left side.
code :
``````1100 >> 2 == 1111
0111 >> 2 == 0001
`````` ## Bitwise operations on big integers in PHP 5.2 and PHP 5.3

By : kkkk089
Date : March 29 2020, 07:55 AM
it fixes the issue When PHP encounters a number which doesn't fit in PHP_INT_MAX (32bits on x86), it will convert the number into a float: ## Is it possible to write a function adding two integers without control flow and strictly bitwise operations?

By : Michael Ellis
Date : March 29 2020, 07:55 AM
wish of those help For a fixed length integer, you can just unroll a ripple carry adder. In the worst case, a carry signal has to propagate all the way from the least significant bit to the most significant bit.
Like this (only slightly tested) (to avoid the C-purists' wrath, I will call this C# code)
code :
``````int add_3bits(int x, int y)
{
int c = x & y;
x = x ^ y;
y = c << 1;
//
c = x & y;  //  \
x = x ^ y;  //  | for more bits, insert more of these blocks
y = c << 1; //  /
//
// optimized last iteration
return (x ^ y) & 7; // for more bits, change that mask
}
``````
``````static uint add_32bits(uint x, uint y)
{
uint p = x ^ y;
uint g = x & y;

g |= p & (g << 1);
p &= p << 1;

g |= p & (g << 2);
p &= p << 2;

g |= p & (g << 4);
p &= p << 4;

g |= p & (g << 8);
p &= p << 8;

g |= p & (g << 16);

return x ^ y ^ (g << 1);
}
`````` ## Is the cost of bitwise operations on 64-bit integers the same as 8-bit integers?

By : user3190786
Date : March 29 2020, 07:55 AM
this will help The number of clock cycles taken on a 64-bit operation is not guaranteed to be 1 even on a 64-bit machine, but obviously the processor doesn't know whether the 64-bit value stands for one 64-bit or eight 8-bit integers, so the bitwise operation itself will be as fast for both cases. This part of the code will also almost certainly perform much better for the single 64-bit value, as the 64-bit processor probably works on 64- (or at least 32-bit) quantities even when you do the operations on smaller variables.
For the overall performance of your program much depends on how often you'd then need to convert between the 8- and 64-bit data; the typical indexing of a single 8-bit integer stored within an array of 64-bit integers would be something like (a[i / 8] >> ((i % 8) * 8)) & 0xFF - so at least on the C side† it would add complexity if done often, but if most of your operations are repeated for all elements of the array then the 64-bit solution is likely to win regardless (bearing in mind that the compiler may have to generate similar masking when working on 8-bit variables anyhow). ## Infinite loop while adding two integers using bitwise operations in Python 3

By : John Lee
Date : March 29 2020, 07:55 AM
To fix the issue you can do Python 3 has arbitrary-precision integers ("bignums"). This means that anytime x is negative, x << 1 will make x a negative number with twice the magnitude. Zeros shifting in from the right will just push the number larger and larger.
In two's complement, positive numbers have a 0 in the highest bit and negative numbers have a 1 in the highest bit. That means that, when only one of a and b is negative, the top bits of a and b will differ. Therefore, x will be positive (1 & 0 = 0) and y will be negative (1 ^ 0 = 1). Thus the new a will be positive (x<<1) and the new b will be negative (y). 