![]() |
| | #1 |
| Registered User Join Date: Oct 2008
Posts: 10
| Bit fields ff_ftime and ff_fdate contain bit fields for referring to the current date and time. The structure of these fields was established by the operating system. Both are 16-bit structures divided into three fields. ff_ftime: Bits 0 to 4 The result of seconds divided by 2 (for example 10 here means 20 seconds) Bits 5 to 10 Minutes Bits 11 to 15 Hours Well, yeah there's ff_fdate too but they're pretty much the same. So... I've checked MSDN what are bit fields - seemed pretty clear, but... as I understood they are used to save some space when using integrals in structures and ff_fdate is not a structure (I think) and I'm lost now. I, basicaly, need to extract a viable time from ff_ftime, but for, what is 21:35:47, I get 44152 and for, what is 15:19:36, I get 31347. Halp. |
| Perspektyva is offline | |
| | #2 |
| Registered User Join Date: Jun 2008
Posts: 1,135
| I am reading the page you find the information. I believe it doesn't mean bit fields in that sense. Just field of bits, meaning that you can use a mask to take the information you want. Like if you want seconds: Code: ff_ftime t; unsigned short SEC_MASK = 15; // 15 = 0..01111 unsigned short sec = t & SEC_MASK; sec *= 2; |
| C_ntua is offline | |
| | #3 |
| Registered User Join Date: Oct 2008
Posts: 10
| It works! But how to get the minutes and hours out? or anything that starts not at the beggining? |
| Perspektyva is offline | |
| | #4 |
| Rampaging 35 Stone Welsh Join Date: Apr 2007
Posts: 2,929
| minutes would be (timecode & 2016) / 32;
__________________ He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet |
| abachler is offline | |
| | #5 |
| Registered User Join Date: Oct 2008
Posts: 10
| didn't you mean & 2047? |
| Perspektyva is offline | |
| | #6 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| And that's why you should always specify bitmasks in hex.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #7 |
| Registered User Join Date: Oct 2008
Posts: 10
| Dude... I'm an amateur, I've started c++ programming 2.5 months ago, before that I only knew only how to create arrays, procedures, functions and write and read from files in pascal. I do not know what bitmasks are, I know what hex is, though. And isn't the time encoded in binary, in this case? |
| Perspektyva is offline | |
| | #8 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| Numbers are always "encoded" in binary, as the computer sees it. That's irrelevant when it comes to the programmer reasoning about them, though. Radix is just a display convention; numbers are pure. The advantage of hex is that there's a very simple mapping to binary: four binary digits form a single hex digit, always. Thus, it's more compact than binary, and easier to translate than decimal. Speaking of binary, here's how bitfields and bitmasks work. Suppose you have a 16-bit integer: 0000000000000000 Now, what's stopping you from reinterpreting that? Say, as two 5-bit and one 6-bit integers? 00000 000000 00000 When you do that, you've turned the integer into a bitfield. A bitmask then is an integer that you can use in bitwise operations (bitwise-AND, &, usually) to assist you in using the bitfield. The bitmask for the first block above is 1111100000000000 For the second block 0000011111100000 And so on. It's pretty simple.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #9 |
| Registered User Join Date: Oct 2008
Posts: 10
| I see, but wouldn't specifying bitmasks in hex would restrict the masks to be 4*n bits in size, in which case, with time firmats, where numbers don't go higher than 60, would cause empty bits of memory that stay 0 all the time? Anyways, I can't get abachlers example to work, do i have to put it like this: Code: unsigned short sec = (t & 2016) / 32; I mean, I understand how the 1 and 0 are placed and I could brew a method that would extract decimals from specific intervals of a binary number, but... what's the shorter way? |
| Perspektyva is offline | |
| | #10 | |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| Quote:
Now, the entire mask is restricted to be of an actually supported integer type, as every other integer. I don't know what you mean by the other part. There's no number you can represent in binary that you can't represent in hex.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law | |
| CornedBee is offline | |
| | #11 | |
| Registered User Join Date: Oct 2008
Posts: 10
| Quote:
A code snippet which would fit nicely in C_ntua's one would be nice. | |
| Perspektyva is offline | |
| | #12 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| Assuming 0-based counting of bits. You either mask first and then shift, or you shift first and then mask. Shifting first is easier, because the mask always starts at the 0th bit. Code: unsigned int packed_time = get_a_packed_time();
unsigned minutes = (packed_time
>> 6) // Shift so that 6th to 11th bit are now 0th to 5th
& 0x1F; // Mask out everything beyond the 5th bit. 0x1F is 0000000000011111
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #13 | |
| Registered User Join Date: Oct 2008
Posts: 10
| Quote:
| |
| Perspektyva is offline | |
| | #14 |
| Rampaging 35 Stone Welsh Join Date: Apr 2007
Posts: 2,929
| no, I didn't, although that woudl work also, I specifically used the number 0000001111100000, although im sure 0000001111111111 woudl also work sicne you are just shifting after that anyway.
__________________ He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet |
| abachler is offline | |
![]() |
| Tags |
| bit fields, ffblk, ff_fdate, ff_ftime, time |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| union in struct with bit fields | sagarnewase | C Programming | 4 | 05-12-2008 07:30 AM |
| How to set an 32 bit allocation to 31 bit for address, 1 bit for flag? | franziss | C Programming | 7 | 12-10-2004 08:18 AM |
| bit patterns of negtive numbers? | chunlee | C Programming | 4 | 11-08-2004 08:20 AM |
| Bit Manipulation Questions | CPPNewbie | C++ Programming | 7 | 08-12-2003 02:17 PM |
| Array of boolean | DMaxJ | C++ Programming | 11 | 10-25-2001 11:45 PM |