## Posts Tagged ‘**tech**’

## What’s wrong with this piece of code ?

struct foo { int a, b; }; void kstrtol(long *x) { *x = -1; } int main(void) { struct foo foo; foo.b = 42; kstrtol((long *)&foo.a); return 0; }

## Application Of Logarithms To Exponential Values And Other Integers

We will be dealing with positive integers and logarithms to base 10.

How do you calculate the no of digits present in an exponential value or for that matter a very huge integer ? The answer is simple as shown below.

no of digits = floor(log (x ^ y)) + 1

**Explanation** : The logarithm of any number to base 10 is the number of times 10 is multiplied with itself to obtain that number. A little experimentation will show that when 10 is multiplied with itself x times or put another way, is raised to power x, we obtain an integer which contains x + 1 digits. Hence, obtaining the base 10 logarithm of any number and adding 1 to it will give you the number of digits in that number. Carefully note the floor function being used around the logarithm as logarithms generally give you floating point or decimal values.

Find the first n digits of an exponential value. Eg – Find first 5 digits of 12^13.

x = log 12^13 = 13 * log 12 = 14.0293561 y = floor(10^(x - floor(x) + n - 1)) = floor(10^(14.0293561 - 14 + 5 - 1)) = floor(10^(4.0293561)) = 10699 y is the first five digits of 12^13.

**Explanation**:

12^13 = 106993205379072.

log 12^13 = 14.0293561.

10^14.0293561 = (10^14)×(10^0.0293561) = (100000000000000)×(10^0.0293561) ≈ 106993205379072

The above argument shows that 10^0.0293561 when multiplied by 10^14 adds 06993205379072 to 100000000000000. In other words, it gives the first 15 digits of 12^13. Hence, if we want the first 5 digits of 12^13 multiply 10^0.0293561 with 10^4 which is also the same as raising 10 to power 4.0293561

**NOTE**: As n increases, the logarithm log x^y also needs to be calculated for more decimal places.

## Find Direction Of Growth Of Stack In C

#include <stdio.h> typedef unsigned long long UINT64; UINT64 f(int b){ return (UINT64)&b; } bool downwards(int a) { int b = 0; if ((&a - f(b)) < 0) return false; else return true; } int main(int argc, char* argv[]) { int a = 0; if (downwards(a)) printf("Downwards\n"); else printf("Upwards"); return 0; }

## Array indexing in C, the unknown kind

int a[5] = { 1, 2, 3, 4, 5 };

printf(“%d\n”, a[3], 3[a]);

output: 4, 4 -> Holy crap! I didn’t know that. I tried it out on the latest version of GCC and it works. Surely, C is like a sea.