Which is faster: x
Potentially depends on the CPU. However, all modern CPUs (x86, ARM) use a “barrel shifter” — a hardware module specifically designed to perform arbitrary shifts in constant time. So the bottom line is… no. No difference.
Potentially depends on the CPU. However, all modern CPUs (x86, ARM) use a “barrel shifter” — a hardware module specifically designed to perform arbitrary shifts in constant time. So the bottom line is… no. No difference.
Vectorization (as the term is normally used) refers to SIMD (single instruction, multiple data) operation. That means, in essence, that one instruction carries out the same operation on a number of operands in parallel. For example, to multiply a vector of size N by a scalar, let’s call M the number of operands that size … Read more
At a low level, there is no such thing as a multi-dimensional array. There is just a flat block of memory, large enough to hold a given number of elements. In C, a multi-dimensional array is conceptually an array whose elements are also arrays. So if you do: int array[2][3]; Conceptually you end up with: … Read more
This is clearly implementation-specific. Below I include the Object.hashCode() implementation used in OpenJDK 7. The function supports six different calculation methods, only two of which take any notice of the object’s address (the “address” being the C++ oop cast to intptr_t). One of the two methods uses the address as-is, whereas the other does some … Read more
The call stack could also be called a frame stack. The things that are stacked after the LIFO principle are not the local variables but the entire stack frames (“calls”) of the functions being called. The local variables are pushed and popped together with those frames in the so-called function prologue and epilogue, respectively. Inside … Read more
In C, with bitwise operators: #include<stdio.h> int add(int x, int y) { int a, b; do { a = x & y; b = x ^ y; x = a << 1; y = b; } while (a); return b; } int main( void ){ printf( “2 + 3 = %d”, add(2,3)); return 0; } … Read more
Editor’s note: this is not actually what compilers do, and gives the wrong answer for large positive integers ending with 9, starting with div10(1073741829) = 107374183 not 107374182. It is exact for smaller inputs, though, which may be sufficient for some uses. Compilers (including MSVC) do use fixed-point multiplicative inverses for constant divisors, but they … Read more
By the C standard, the compiler is free to store the bit field pretty much in any random way it wants. You can never make any assumptions of where the bits are allocated. Here are just a few bit-field related things that are not specified by the C standard: Unspecified behavior The alignment of the … Read more