In software engineering, often developers will utilize three different states of a signed integer, as a trilean
:
This tends to be quite typical:
-1 - Represents something akin to a null pointer, as in it has no initiation or use or meaning
0 - Represents zero or false
1+ - Represents a valid value
In this case of QIODevice::waitForBytesWritten():
-1 - Wait Forever
0 - Do not wait at all
1+ - Wait for n
Given this, I have wondered about the viability and usefulness of an integral type whose bottom range was -1, and what value that would represent. I would imagine it would be something like,
unsigned char quint8(0)
h :: 0
0000 0000
unsigned char quint8(1)
h :: 1
0000 0001
unsigned char quint8(2)
h :: 2
0000 0010
unsigned char quint8(253)
h :: 253
1111 1101
unsigned char quint8(254)
h :: 254
1111 1110
unsigned char quint8(255)
h :: -1 // The change being here
1111 1111
unsigned char quint8(256)
h :: 0
0000 0000
1: Does an integral type like this already exist somewhere?
Has it been implemented? Initially I had thought that ssize_t
had operated something like this, but I recall being mistaken on that presumption.
2: What are its advantages?
The obvious advantages would be a higher cap, at 254 as opposed to 128 The other main advantage I would see, would be a greater ability to be explicit when writing code. If a parameter for example accepts an integral type like this, then that tells me before hand that -1 has a particular meaning.
I also imagine that there would be some advantages as far as bitwise operators were concerned, but I have not ruminated enough to think of any.
3: What are its disadvantages?
4: Is it simply too complex or difficult or inefficient to implement, and is that fundamentally why it does not exist as a common type?
X..Y
) or offset int types for a0..2^(8*x)+-Y
allowing for example to store-1000..-745
in a byte offset by-1000
. As for why these are not common? Silicon is the fastest, and most silicon implement unsigned, two's complement, and/or one's complement. Everything else is slower because its done in software.uint8_t
. With the modular arithmetic on unsigned numbers, 255 = -1.