As already mentioned, the number of bits required to represent two numbers in the base can be calculated using logarithms. For example, the following code in Erlang.
bits(0) ->
1;
bits(X) when is_number(X), X < 0 ->
1 + bits(erlang:abs(X));
bits(X) when is_number(X) ->
erlang:trunc(math:log(X) / math:log(2) + 1).
If you are only interested in words of sizes 1,2 and 4, then, of course, it’s nice to check only a few restrictions. I like to use base 16 for limits using the Rhlar Erlang notation.
unsigned_wordsize(X) when is_integer(X), X >= 0 ->
case X of
_ when X =< 16
_ when X =< 16
_ when X =< 16
end.
, . , .
signed_wordsize(X) when is_integer(X), X < 0 ->
signed_wordsize(-(X+1));
signed_wordsize(X) when is_integer(X) ->
case X of
_ when X =< 16
_ when X =< 16
_ when X =< 16
end.