this post was submitted on 04 Oct 2024
4 points (83.3% liked)

The C Programming Language

632 readers
10 users here now

Everything related to the C programming language.

founded 5 years ago
MODERATORS
 

Second representation requires at least 6 bits to represent numbers between 0 to 59. But 5 bits are not just enough - 2^5^ = 32, which can only represent from 0 up to 31 seconds.

According to K.N. King:

You may be wondering how it 's possible to store the seconds - a number between 0 and 59 in a field with only 5 bits. Well. DOS cheats: it divides the number of seconds by 2, so the seconds member is actually between 0 and 29.

That really makes no sense?

you are viewing a single comment's thread
view the rest of the comments
[–] velox_vulnus@lemmy.ml 0 points 1 day ago (1 children)

But during the release of MS-DOS, there were only 16-bit microprocessors, right? 32-bit x86 processors came way later around 1985, I think?

[–] FigMcLargeHuge@sh.itjust.works 2 points 1 day ago (1 children)

I don't know the specifics, but why would 5 bits be a problem on a 16bit machine? Shit, my mistake, I should have said 'If you have 5 bits to work with'. I will correct it.

[–] velox_vulnus@lemmy.ml 2 points 1 day ago* (last edited 1 day ago) (2 children)

I don’t know the specifics, but why would 5 bits be a problem on a 16bit machine?

Because I'm assuming that there's only 16 bits, right, and hence these have to be divided between hours, minutes and seconds:

  • 5 bits for the hours (2^5^ = 32, to represent numbers from 0-23)
  • 6 bits for the minutes (2^6^ = 64, can represent numbers from 0-59)
  • the 5 bits for the seconds (2^5^ = 32, can only represent numbers from 0-31, what about seconds 32-59?)

5 + 6 + 5 = 16 bits in total. This is why the last one makes no sense, unless the timeformat precision was restricted to 2 seconds?

Yeah, I have been trying to work out a table for just the seconds, and yeah, the 5 bits isn't enough. I wasn't thinking it through. Another bit would be needed to be able to capture the flip, and in that case the extra bit would just allow you to store the actual 0-59 value. That's what I get for speaking before I have worked it out in my head.

[–] j4k3@lemmy.world 1 points 1 day ago

Probably need to go as deep as the ALU operations available and clock cycles to really see what is going on.

I know nearly nothing of value in this space, but have seen lots of little tricks like this in passing with hobbyist hardware or ROMs. Intuitively, it feels like one of those kinds of situations. You might even find there is a clever use of a carry flag or something of that nature where there is essentially a free bit on an operation. I'm not sure how x86 does the ALU or whatnot here or even if this advice is relevant with an overlay tree. Feel free to inform/correct.