Mathematical definition of the function to extract a value in an integer by bit selection

binarybinary operations

I need the definition of a function $s(v,p,w) = i$ where $v$ is an integer that can be expressed as binary or decimal, $p$ is the position of a bit in $v$, and $w$ is an amount of bit from 1 to $n$. The function return $i$, another integer that corresponds to the $w$'s bits at position $p$ in $v$.

Examples with $v = $ 0b1011 = 11

For $p = 0$ and $w = 1$, $f(11, 0, 1) = 1$

$f(11, p=1, w=1) = 1$

$f(11, p=0, w=2) = 3 = $ 0b11

$f(11, p=1, w=2) = 1 = $ 0b01

etc. (sorry for abusive notations).

this post partially answer by returning a given bit, but I also need the bit-width ads parameter.

Best Answer

Just looking at your link, I think you can adapt the answer that way:

$$f(b,k,w)=\left\lfloor\frac{b}{2^k}\right\rfloor-2^w\left\lfloor\frac{b}{2^{k+w}}\right\rfloor$$