OSDev.orghttps://forum.osdev.org/ Using -1 with unsigned integers only as missing markerhttps://forum.osdev.org/viewtopic.php?f=13&t=32785 Page 1 of 1

 Author: ~ [ Tue Mar 06, 2018 6:24 pm ] Post subject: Using -1 with unsigned integers only as missing marker I think that it would be much better to use -1 with unsigned int or unsigned long instead of using their signed variants.The idea is to reserve the value -1 (all bits set to 1) solely to indicate a missing offset, but use unsigned integers for the rest of values as it should be, to avoid dividing the capacity of an offset to its half.It just takes away 1 single unsigned number to be reserved similarly to NULL, so why isn't it normally implemented like this. I think that I will implement all of my offsets as unsigned integers and I will only reserve the unsigned value of -1 for missing offsets.Nobody said to use unsigned offsets, all they said was to use -1 (all bits to 1) as the marker for a missing offset.

 Author: Brendan [ Tue Mar 06, 2018 7:33 pm ] Post subject: Re: Using -1 with unsigned integers only as missing marker Hi,~ wrote:I think that it would be much better to use -1 with unsigned int or unsigned long instead of using their signed variants.The idea is to reserve the value -1 (all bits set to 1) solely to indicate a missing offset, but use unsigned integers for the rest of values as it should be, to avoid dividing the capacity of an offset to its half.It just takes away 1 single unsigned number to be reserved similarly to NULL, so why isn't it normally implemented like this. I think that I will implement all of my offsets as unsigned integers and I will only reserve the unsigned value of -1 for missing offsets.Nobody said to use unsigned offsets, all they said was to use -1 (all bits to 1) as the marker for a missing offset.C is designed to be portable to different computers; where some computers use "two's complement" for signed numbers (where -1 == 0xFFFFFFFF), some computers use "sign and magnitude" (where -1 == 0x80000001), and some computers use something else (where -1 == something else). More correctly, something like "unsigned int x = -1;" relies implementation defined behaviour, where implementation defined behaviour is only slightly better than undefined behaviour.For this reason, a good C programmer won't use -1 for unsigned integers - they'd use (e.g.) UINT_MAX instead to ensure that it's portable (and ensure that everyone doesn't think their code is bad even when portability doesn't matter).The next problem is that this value doesn't survive promotion. For example, if "bar()" returns an unsigned int and you do "unsigned long foo = bar(); if (foo == ULONG_MAX) {..." then you might or might not get bugs depending on the actual sizes (e.g. if "int" is 32-bit and "long" is 64-bit; then the 32-bit value 0xFFFFFFFF might be converted to the 64-bit value 0x00000000FFFFFFFF and won't match ULONG_MAX; but if "int" and "long" are both 32-bit the code will work). In other words; this can easily become another "implementation defined" portability disaster.The next problem is that a good interface may return more than just one kind of error (note: admittedly the C standard library is pure trash, doesn't use good interfaces and ends up relying on the horrible "errno" mess for the same purpose). In this case, for extensibility, it'd make sense to use a range of values for "errors of any kind" and another range for "no error"; and the easiest way to do that is to use signed integer where negative values indicate errors. For example, something like -1 = bad parameters, -2 = not enough memory, -3 = permission denied, -4 = file not found, etc.The final problem is that nobody has a clue what this is actually for. Something that's ideal for one thing might be completely inappropriate for something else. Sometimes you might want errors to cause immediate termination, or cause a signal, or throw an exception; so that the programmer can assume that errors are never returned from functions. Sometimes negative values may be valid and can't be used to indicate errors. Sometimes you might want to return "status" even when there was no error (e.g. "succeeded without problem" and "succeeded, but an error occurred and was corrected"). It's a little bit like saying "I think people should smile and laugh more" - it sounds nice if you don't spend any time thinking about the corner-cases (e.g. funerals).Cheers,Brendan

 Author: StudlyCaps [ Tue Mar 06, 2018 10:58 pm ] Post subject: Re: Using -1 with unsigned integers only as missing marker ~ wrote:I think that it would be much better to use -1 with unsigned int or unsigned long instead of using their signed variants.The idea is to reserve the value -1 (all bits set to 1) solely to indicate a missing offset, but use unsigned integers for the rest of values as it should be, to avoid dividing the capacity of an offset to its half.It just takes away 1 single unsigned number to be reserved similarly to NULL, so why isn't it normally implemented like this. I think that I will implement all of my offsets as unsigned integers and I will only reserve the unsigned value of -1 for missing offsets.Nobody said to use unsigned offsets, all they said was to use -1 (all bits to 1) as the marker for a missing offset.What if you had something at offset of MAX_INT? Cause valid ranges often go 0 to MAX_

 Author: alexfru [ Wed Mar 07, 2018 12:01 am ] Post subject: Re: Using -1 with unsigned integers only as missing marker Brendan wrote:More correctly, something like "unsigned int x = -1;" relies implementation defined behaviour, where implementation defined behaviour is only slightly better than undefined behaviour.For this reason, a good C programmer won't use -1 for unsigned integers - they'd use (e.g.) UINT_MAX instead to ensure that it's portable (and ensure that everyone doesn't think their code is bad even when portability doesn't matter).(unsigned int)-1 equals UINT_MAX. See the "Representations of types" and "Conversions" sections of the C standard.

 Author: Solar [ Wed Mar 07, 2018 3:59 am ] Post subject: Re: Using -1 with unsigned integers only as missing marker @alexfru is correct -- "unsigned int x = -1" is a portable way to get all bits set in x. "unsigned int x = UINT_MAX" is, of course, more expressive.As for what ~ originally wrote... C++ does that (e.g. "std::string::npos" is defined as "static_cast(-1)"). If you're using "signed int" as an index, you're doing it wrong already -- as e.g. strlen() returns the unsigned type size_t, and you (hopefully) start getting signed / unsigned comparison warnings from your compiler, which you will (hopefully) not ignore. An offset is a different thing from an index, and can be negative.Mind your vocabulary. Say what you mean, mean what you say.

 Author: ~ [ Wed Mar 07, 2018 6:06 am ] Post subject: Re: Using -1 with unsigned integers only as missing marker Doesn't the compiler generate the right sequence for -1 and the other values whenever it's specified anyway, regardless of using it as an unsigned bit pattern or as an actual signed integer?

 Author: Solar [ Wed Mar 07, 2018 7:22 am ] Post subject: Re: Using -1 with unsigned integers only as missing marker Errr... I beg your pardon?Could you re-phrase that question? Because I have no idea what you just asked.

 Author: Schol-R-LEA [ Wed Mar 07, 2018 7:52 pm ] Post subject: Re: Using -1 with unsigned integers only as missing marker Sorry for the sequential posts; I had to leave for an appointment earlier, before I had a chance to finish what i mean to say, and since I am uncertain if ~ read the previous post or not (the timing is really close to when i last edited it, meaning he may have seen the first posted version but not the fixes) thought it would be better to bite the bullet and post again.Anyway, the rest of the story here is that, as Brendan said earlier, it isn't entirely clear what you are trying to accomplish in the first place. What is the use case for this? What is the context of the question? What 'missing offsets' are you concerned about? I mean, honestly, you didn't ask a question, or even make a coherent statement really, you just sort of started into a something as if you were halfway through a conversation. What do you expect us to make of that? (A paper hat, perhaps... I may be dating myself with that reference, but whatev'.)In the absence of other information I would say, bundle the value into a struct with a Boolean flag to indicate a valid/invalid value, Code:typedef struct GOFFSET {    bool valid;    unsigned int offset;          // note that the exact size may not matter here        } guarded_offset;but I have no idea if that's even an applicable solution. You might use a struct with a bit field instead, if you know how the compiler will lay the values out (or don't need to know), but that can lead to a number of unexpected compiler dependencies. Code:typedef struct BF_GOFFSET {    bool valid: 1;    uint32_t offset: 31;              // ... but it definitely does here} bitfield_guarded_offset;

 Author: ~ [ Thu Mar 08, 2018 5:35 am ] Post subject: Re: Using -1 with unsigned integers only as missing marker The use case is an offset as big as possible, on disk, memory, etc...I wouldn't like to have, say, 31 bits available for a file offset just to reserve -1 (specifically unsigned 0xFFFFFFFF or 0xFFFFFFFFFFFFFFFF, depending on the register width) as an "offset not found". I could just use an unsigned integer, use the full 32 or 64-bit range and just reserve -1 as a simple bit pattern generated by the compiler to signal "offset not found". 0 is a valid offset, -1 would be like NULL unless we return more than a value at once from a function to check an error/success flag (probably cleaner than reserving special values, like the CPU or BIOS do on error (CF, etc...) and more portable down to the machine level?)

 Author: Solar [ Thu Mar 08, 2018 5:44 am ] Post subject: Re: Using -1 with unsigned integers only as missing marker This is already being done, using unsigned as index (not offset!) and -1 ("all bits set") as "fault" indicator.Have you read what was written by others?No. As usual, you just keep harping on.

 Author: bluemoon [ Thu Mar 08, 2018 7:05 am ] Post subject: Re: Using -1 with unsigned integers only as missing marker ~ wrote:31 bits available for a file offsetWait, this put a 2GB limit on file size. Consider use full 64-bit.

 Author: Pype.Clicker [ Thu Mar 08, 2018 8:58 am ] Post subject: Re: Using -1 with unsigned integers only as missing marker I'd be tempted to useCode:INVALID_WHATEVER=~0U/*LL*/for unsigned /* long long */ types.

 Author: Solar [ Thu Mar 08, 2018 10:29 am ] Post subject: Re: Using -1 with unsigned integers only as missing marker Pype, consider the StackOverflow link I posted earlier. ~0 works for two's complement, but not for others...