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

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 Cpurists' 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 64bit integers the same as 8bit integers?
By : user3190786
Date : March 29 2020, 07:55 AM
this will help The number of clock cycles taken on a 64bit operation is not guaranteed to be 1 even on a 64bit machine, but obviously the processor doesn't know whether the 64bit value stands for one 64bit or eight 8bit 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 64bit value, as the 64bit processor probably works on 64 (or at least 32bit) 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 64bit data; the typical indexing of a single 8bit integer stored within an array of 64bit 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 64bit solution is likely to win regardless (bearing in mind that the compiler may have to generate similar masking when working on 8bit 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 arbitraryprecision 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).

