educative.io

Educative

**What could be the size of our PhotoID?

What could be the size of our PhotoID? Let’s say our epoch time starts today; how many bits we would need to store the number of seconds for the next 50 years?

86400 sec/day * 365 (days a year) * 50 (years) => 1.6 billion seconds

We would need 31 bits to store this number. Since, on average, we are expecting 23 new photos per second, we can allocate 9 additional bits to store the auto-incremented sequence. So every second, we can store (2^9 => 512) new photos. We are allocating 9 bits for the sequence number which is more than what we require; we are doing this to get a full byte number (as 40 bits = 5 bytes). We can reset our auto-incrementing sequence every second.

Can someone explain why 31 bits are needed to store the number? Why is the calculation “2^9 => 512” relevant?

1 Like

PhotoID = EpochNumber + sequencingNumber

let’s go sentence by sentence:

We would need 31 bits to store this number. => to get bits we can use the below formula
bits = floor(log n/log2) +1
so n here is 1.6 billion so if you do the above calculation you get 31, so you need 31 bits to store Epoch time for 50 years.

we are expecting 23 new photos per second, => for 23 new photos you can use below logic:

  • use 5 bits, because 2^5 = 32
  • use 6 bits, because 2^6 = 64
  • use 7 bits because 2^7 = 128
  • use 8 bits because 2^8 = 256
  • use 9 bits, because 2^9 = 512
    if you use 5 bits, the total number of bits for photoID will be 31 +5 = 36 bits = 4.5 bytes
    if you use 6 bits, the total number of bits for photoID will be 31 +6 = 37 bits = 4.625 bytes
    if you use 7 bits, the total number of bits for photoID will be 31 +7 = 38 bits = 4.75 bytes
    if you use 8 bits, the total number of bits for photoID will be 31 +8 = 39 bits = 4.875 bytes
    But if you use 9 bits, the total number of bits for photoID will be 31 +9 = 38 bits = 5 bytes
    so if we can choose 9 bits, as the sequencing number to present 23 photos/sec, so you can have a full byte number (5 Byte = 40 bits)