N
N
NikSIk312019-05-02 22:24:07
Programming
NikSIk31, 2019-05-02 22:24:07

How to find the length of a number in bits?

Can you please explain what is the "length of a number in bits", what should be done to determine it?
Let's say Wikipedia says that I need to take a number of 1024 bits, and below is an example, and there is a number, only it seems to me that they have it less.
Please tell us how it works?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
jcmvbkbc, 2019-05-02
@NikSIk31

how is it "the length of the number in bits", what should be done to determine it.

This is the number of bits required to write the number.
You need to take the next larger integer after the base 2 logarithm of the number. 1 -- 1 bit, 2 -- 2 bits, 3 -- 2 bits, 4 -- 3 bits... 1023 -- 10 bits, 1024 -- 11 bits... ) -- 1024 bits, 179,769,313,486,231,590,772,930,...,304,835,356,329,624,224,137,216 -- 1025 bits...

M
Max Goncharenko, 2019-05-03
@reverse_kacejot

To determine how many bits you need at least to store a number, you need to take its logarithm to base 2 (you first need to add 1, because we also take into account 0). If the result is not an integer (the number is not a power of 2), then it is worth rounding it up:
log2(255 + 1) = 8 - then the number will fit into 8 bits
log2(256 + 1) = 8.0056... - round up to 9
This scheme does not work for 0. You need to remember that it also needs one bit :)

N
nrgian, 2019-05-02
@nrgian

128 byte integer?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question