Getting the nth bit of a decimal number

779 Views Asked by At

I have a formula for decoding a 3-bit data object: $$T = 68 + 2 \sum_{i=0}^22^iTempA_i$$ where $TempA$ is the 3-bit object and $TempA_i$ is the $i$'th bit from the right.

I am trying to rewrite this as a formula for $TempA_i$ and I understand how to do this with a C program (I'm a a programmer, not a mathematician) but I can't determine if it is possible to write a non-conditional formula for this. I believe I need to use the modulus operator to achieve the formula, but I haven't had any luck yet.

1

There are 1 best solutions below

7
On BEST ANSWER

A formula for each digit:

$Temp{A_0}=\dfrac{T-68}{2}\bmod2$

$Temp{A_1}=\big\lfloor\dfrac{T-68}{4}\big\rfloor\bmod2$

$Temp{A_2}=\big\lfloor\dfrac{T-68}{8}\big\rfloor\bmod2$

The general formula is:

$Temp{A_i}=\big\lfloor\dfrac{T-68}{2^{i+1}}\big\rfloor\bmod2$


In C, a simple integer-division can be used instead of the $\big\lfloor\dots\big\rfloor$:

TempA0 = ((T-68)/2)%2;
TempA1 = ((T-68)/4)%2;
TempA2 = ((T-68)/8)%2;
TempA[i] = ((T-68)/(1<<(i+1))%2;

As suggested in one of the comments below, bit-wise operations can be used instead:

TempA0 = ((T-68)>>1)&1;
TempA1 = ((T-68)>>2)&1;
TempA2 = ((T-68)>>3)&1;
TempA[i] = ((T-68)>>(i+1))&1;