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.

Why is vectorization, faster in general, than loops?

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

How are 3D arrays stored in C?

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

What is an “internal address” in Java?

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

How exactly does the callstack work?

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

Divide by 10 using bit shifts?

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

tech