As mentioned, the number of bits needed to represent a number in base two can be calculated using logarithms. Such as 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 concerned with word sizes 1,2 and 4, then it is of course nice to check only the few limits. I like to use base 16 for the limits using Erlang's radix notation.
unsigned_wordsize(X) when is_integer(X), X >= 0 ->
case X of
_ when X =< 16#000000ff -> 1;
_ when X =< 16#0000ffff -> 2;
_ when X =< 16#ffffffff -> 4
end.
And I assume from your code that you are using two's complement for signed integers. So I map negative numbers over to positive so I can use the same table.
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#0000007f -> 1;
_ when X =< 16#00007fff -> 2;
_ when X =< 16#7fffffff -> 4
end.