Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!philabs!prls!pyramid!pesnta!wjvax!brett From: brett@wjvax.UUCP (Brett Galloway) Newsgroups: net.lang.c Subject: Re: boolean datatype Message-ID: <705@wjvax.wjvax.UUCP> Date: Wed, 4-Jun-86 12:21:28 EDT Article-I.D.: wjvax.705 Posted: Wed Jun 4 12:21:28 1986 Date-Received: Fri, 6-Jun-86 04:58:20 EDT References: <5498@alice.uUCp> <1462@mmintl.UUCP> <852@bentley.UUCP> <1126@whuxl.UUCP> <393@peregrine.UUCP> Reply-To: brett@wjvax.UUCP (Brett Galloway) Organization: Watkins-Johnson Co., San Jose, Calif. Lines: 47 In article <393@peregrine.UUCP> mike@peregrine.UUCP (Mike Wexler) writes: >Another feature I would like to have is a range data type. I don't >know of a good syntax for putting this in C but it would be nice to >use in programs where are know the possible values of a number but >would like the compiler to figure out an efficient sized slot to put >it in. > ... > range hour:1..12; /* create a variable named hour that can vary from */ > typedef range DAY:1..366; > DAY day1,day2; > >I will let other people argue about how to index an array by a range. I would prefer an operator that merely generated an integral type based on a number of bits requested, not based on a desired range. For example, on a machine with 8-bit char, 16-bit short, 32-bit int, and 64-bit long, bittype(8) == char; bittype(9) == short; bittype(31) == int; and so on. Bittype() would be evaluated at the same time sizeof() is evaluted, which I think is in the preprocessor. The preprocessor would just substitute a type word (char, int, etc.) for the bittype(n) operator. Bittype() would NOT, however, enforce range restrictions; it would just efficiently generate a type that contains at least the given number of bits, if possible. Another operator that would be useful in conjunction with the above and useful elsewhere would be a constant-generation construct that provides knowledge of the number of bits in each of the integral arithmetic types (char, short, int, and long). For example, bitlen(int) == 32 on a 32-bit machine; bitlen(char) == 8 most of the time. This would simplify generating efficient code to simulate bitfield arrays. For example, to generate a packed 100-element array of unsigned 1-bit fields, one could define an array unsigned int wordarray[(100+(bitlen(int)-1))/bitlen(int)]; and define macros to do the bitmask accesses. In fact, the user could write a general macro package to handle all this. My understanding of the current situation is that, short of a well-known #define in an #include file, the only way to determine the bit-length of an integral type is at run-time, by right/left-shifting a bit through an item of that type. Run-time determination is much less efficient, however. It requires the overhead of the run-time determination, plus it precludes compile-time constant evaluation. Finally, while we're on the subject of wish-lists, I think a typeof() operator would be VERY useful. ------------- Brett Galloway {pesnta,twg,ios,qubix,turtlevax,tymix,vecpyr,certes,isi}!wjvax!brett