November 01, 2003
Boolean algebra is a distinct and separate algebra from real or integer algebras.  It supports a much smaller set of operations and possible values. I guess the closest numeric representation would be a modulo-2 integer arithmetic.  And while I would ordinarily want D to as much as possible allow us as users to create such low-level types ourselves, this still falls into the category of "everyone will want it", and it's a good idea to standardize such a thing before too many incompatible implementations are created.

Boolean            Numeric Equivalent
===================
and                 * or min
or                      max or saturating +
xor                  modular +

But not many of the other operations (division, etc) make sense in a boolean context.  For instance, what boolean value would you use to represent the result of true divided by false?  ;)  Implicit conversions, while handy, can also lead to hard-to-find logical errors that the compiler won't warn you about.  After having worked with very low level assembly, I find it annoying that C/C++ always promote the result of any sized integer operation to int or long... that means the semantics of certain operations cannot be supported by hardware byte registers;  it's constantly promoting the bytes up to int behind your back.  That kind of thing can sure impact the performance of a tight inner loop.

Bit, as a concept, is also a quite valuable tool, having many potential uses above and beyond what D currently supports.  I believe I rambled on about such a while back.  For instance, named slices of a bit array could be used as bitfields, if conversion between bit array slice and integral types were allowed.

Sean

"Walter" <walter@digitalmars.com> wrote in message news:bnvk4b$1l5k$1@digitaldaemon.com...
>
> "Matthew Wilson" <matthew-hat@-stlsoft-dot.-org> wrote in message news:bnv224$siq$1@digitaldaemon.com...
> > > > If so, give me a strong bool type!
> > I refer the honourable gentleman to the presumptuous directive I gave so
me
> > moments ago
>
> I understand how you feel about it, but I think we'll just have to agree
to
> disagree!


November 01, 2003

"Sean L. Palmer" wrote:
> 
> Boolean algebra is a distinct and separate algebra from real or integer algebras.  It supports a much smaller set of operations and possible values. I guess the closest numeric representation would be a modulo-2 integer arithmetic.  And while I would ordinarily want D to as much as possible allow us as users to create such low-level types ourselves, this still falls into the category of "everyone will want it", and it's a good idea to standardize such a thing before too many incompatible implementations are created.
> 
> Boolean            Numeric Equivalent
> ===================
> and                 * or min
> or                      max or saturating +
> xor                  modular +

wouldn't it be nice to include such things as

  nand
  nor

given their real world importance?

-- 
Helmut Leitner    leitner@hls.via.at
Graz, Austria   www.hls-software.com
November 01, 2003
Indeed, booleans are _not_ numbers, they're _booleans_ there should be no implicit conversions in either direction. The common use of the symbols "0" and "1" to indicate the boolean values is not to be taken as anything but a convenience, which is why we as nerds usually use "false" and "true".

Boolean should be strongly typed and I don't care about how it is represented behind the scenes. If you also want a one-bit number (whatever that's worth) then you have the bit. But a bit is _not_ a boolean!

In article <3FA3964B.1881EEC7@chello.at>, Helmut Leitner says...
>
>
>
>"Sean L. Palmer" wrote:
>> 
>> Boolean algebra is a distinct and separate algebra from real or integer algebras.  It supports a much smaller set of operations and possible values. I guess the closest numeric representation would be a modulo-2 integer arithmetic.  And while I would ordinarily want D to as much as possible allow us as users to create such low-level types ourselves, this still falls into the category of "everyone will want it", and it's a good idea to standardize such a thing before too many incompatible implementations are created.
>> 
>> Boolean            Numeric Equivalent
>> ===================
>> and                 * or min
>> or                      max or saturating +
>> xor                  modular +
>
>wouldn't it be nice to include such things as
>
>  nand
>  nor
>
>given their real world importance?
>
>-- 
>Helmut Leitner    leitner@hls.via.at
>Graz, Austria   www.hls-software.com


November 01, 2003
Yeah, but I couldn't think of anything in integer arithmetic to correspond to those!  ;)

I refer the interested reader to a more respectable authority on the subject:

http://mathworld.wolfram.com/BooleanFunction.html

http://mathworld.wolfram.com/BooleanAlgebra.html

This blurb emphasises the importance of NAND:

"The NAND operation is the basic logical operation performed by the solid-state transistors ("NAND gates") that underlie virtually all integrated circuits and modern computers. The first axiom system based on NAND was given by Henry Sheffer in 1913. In their landmark tome, Whitehead and Russell (1927) promoted NAND as the appropriate foundation for axiomatic logic. "


I know that personally I find (A AND B) and (A AND NOT B) very useful for dealing with masks.  The closest integer operation to this would be saturating subtraction.  In my day-to-day work I rarely feel the need for NAND though.

const uint Rmask = 0x000000FF;
const uint Gmask = 0x0000FF00;
const uint Bmask = 0x00FF0000;
const uint Amask = 0xFF000000;

const uint Rshift = 0;
const uint Gshift = 8;
const uint Bshift = 16;
const uint Ashift = 24;

uint RGBA(ubyte r,ubyte g,ubyte b,ubyte a)
{
    return (r << Rshift) | (g << Gshift) | (b << Bshift) | (a << Ashift);
}

uint SetR(uint color, ubyte newR)
{
    return (color & ~Rmask) | (r << Rshift);
}
uint SetG(uint color, ubyte newG)
{
    return (color & ~Gmask) | (r << Gshift);
}
uint SetB(uint color, ubyte newB)
{
    return (color & ~Bmask) | (r << Bshift);
}
uint SetA(uint color, ubyte newA)
{
    return (color & ~Amask) | (r << Ashift);
}

Sean

"Helmut Leitner" <helmut.leitner@chello.at> wrote in message news:3FA3964B.1881EEC7@chello.at...
>
>
> "Sean L. Palmer" wrote:
> >
> > Boolean algebra is a distinct and separate algebra from real or integer algebras.  It supports a much smaller set of operations and possible
values.
> > I guess the closest numeric representation would be a modulo-2 integer arithmetic.  And while I would ordinarily want D to as much as possible allow us as users to create such low-level types ourselves, this still
falls
> > into the category of "everyone will want it", and it's a good idea to standardize such a thing before too many incompatible implementations
are
> > created.
> >
> > Boolean            Numeric Equivalent
> > ===================
> > and                 * or min
> > or                      max or saturating +
> > xor                  modular +
>
> wouldn't it be nice to include such things as
>
>   nand
>   nor
>
> given their real world importance?


November 02, 2003
Let me join the current meeting of the bool gang, guys... ;)

(However, also let me thank Walter for at least the bit --> bool alias thing. This makes inconsistent code at least syntactically close to the right thing...)

Sz.

"Juan C" <Juan_member@pathlink.com> wrote in message news:bo0nv2$6vp$1@digitaldaemon.com...
> Indeed, booleans are _not_ numbers, they're _booleans_ there should be no implicit conversions in either direction. The common use of the symbols "0" and "1" to indicate the boolean values is not to be taken as anything but a convenience, which is why we as nerds usually use "false" and "true".
>
> Boolean should be strongly typed and I don't care about how it is represented behind the scenes. If you also want a one-bit number (whatever that's worth) then you have the bit. But a bit is _not_ a boolean!
>
> In article <3FA3964B.1881EEC7@chello.at>, Helmut Leitner says...
> >
> >
> >
> >"Sean L. Palmer" wrote:
> >>
> >> Boolean algebra is a distinct and separate algebra from real or integer algebras.  It supports a much smaller set of operations and possible values. I guess the closest numeric representation would be a modulo-2 integer arithmetic.  And while I would ordinarily want D to as much as possible allow us as users to create such low-level types ourselves, this still falls into the category of "everyone will want it", and it's a good idea to standardize such a thing before too many incompatible implementations are created.
> >>
> >> Boolean            Numeric Equivalent
> >> ===================
> >> and                 * or min
> >> or                      max or saturating +
> >> xor                  modular +


November 02, 2003
Matthew Wilson wrote:

> I personally think that bool should be a distinct type, of the size of int,
> and which cannot engage in implicit conversions to and from integer types.
> Sadly, my sway is marginal, at best. :(

Do you really need to unfold a discussion again? This will lead nowhere.  An alias does the job.

Besides, people shall want to have an array of boolean, and will want to behave like a bitarray, or rather like an array of flags! Bit and bool should be the same. Divorcing them will also introduce a tiny but unrequiered conceptual complexity into the language, and will open a great deal of other questions, as to what conversion rules should be! It's like touching a wesp nest.

After all, there are so many *important* issues! We even have a faulty division operator, a falty switch, and a ton of other faulty C leftovers, which shall not further be dealt with, for obvious reasons. And the bool question has long reached a fragile consensus.

-eye

November 02, 2003
Well, firstly, my post was a long time ago. Several weeks at least

Second, implicit conversions to/from numeric and non-numeric types is a flaw, just as much as switch, etc. Just because a majority of people think that ease of coding is more important than correctness and maintainability, does not mean the issue is moot, merely boring and unpopular.


"Ilya Minkov" <minkov@cs.tum.edu> wrote in message news:bo31b9$a7o$1@digitaldaemon.com...
> Matthew Wilson wrote:
>
> > I personally think that bool should be a distinct type, of the size of
int,
> > and which cannot engage in implicit conversions to and from integer
types.
> > Sadly, my sway is marginal, at best. :(
>
> Do you really need to unfold a discussion again? This will lead nowhere.
>   An alias does the job.
>
> Besides, people shall want to have an array of boolean, and will want to behave like a bitarray, or rather like an array of flags! Bit and bool should be the same. Divorcing them will also introduce a tiny but unrequiered conceptual complexity into the language, and will open a great deal of other questions, as to what conversion rules should be! It's like touching a wesp nest.
>
> After all, there are so many *important* issues! We even have a faulty division operator, a falty switch, and a ton of other faulty C leftovers, which shall not further be dealt with, for obvious reasons. And the bool question has long reached a fragile consensus.
>
> -eye
>


November 03, 2003
So just say that bool/bit store as one machine word, which contains up to 32 or so bools in the same word.  That takes care of storage.  As Walter knows, a bit* is a regular pointer plus as many bits as are required to address the number of bits in a machine word., log2(n), as is a reference to bit.  This is one of the reasons you cannot assume a pointer to be castable back and forth between int and pointer types without losing information  The bit itself is either 0 or 1 and always lives in an addressible hardware bit somewhere, either a cpu flag or memory location.

Conversion from bit to larger types it seems should be possible, but not implicitly?  Maybe it should act more like a boolean and less like a 1-bit integer.  If you want to do integer math on a bit, cast to int (0 or 1) or uint (0 or ~0/) and maybe explicitly specify the true and false results. Maybe the ? : operator could be the only way to convert a bit to an integer, and compilers expected to "figure out" how to do things like (a < b) ? -1 : 0 as efficiently as possible (use setlt, decrement).

I'm for rationals as the result of integer division, the rationals being implicitly convertible to int and return the int portion of the result of the division operation.

Sean



"Ilya Minkov" <minkov@cs.tum.edu> wrote in message news:bo31b9$a7o$1@digitaldaemon.com...
> Matthew Wilson wrote:
>
> > I personally think that bool should be a distinct type, of the size of
int,
> > and which cannot engage in implicit conversions to and from integer
types.
> > Sadly, my sway is marginal, at best. :(
>
> Do you really need to unfold a discussion again? This will lead nowhere.
>   An alias does the job.
>
> Besides, people shall want to have an array of boolean, and will want to behave like a bitarray, or rather like an array of flags! Bit and bool should be the same. Divorcing them will also introduce a tiny but unrequiered conceptual complexity into the language, and will open a great deal of other questions, as to what conversion rules should be! It's like touching a wesp nest.
>
> After all, there are so many *important* issues! We even have a faulty division operator, a falty switch, and a ton of other faulty C leftovers, which shall not further be dealt with, for obvious reasons. And the bool question has long reached a fragile consensus.
>
> -eye


November 04, 2003
In article <bo39ao$m2h$1@digitaldaemon.com>, Matthew Wilson says...
>
>Well, firstly, my post was a long time ago. Several weeks at least
>
>Second, implicit conversions to/from numeric and non-numeric types is a flaw, just as much as switch, etc. Just because a majority of people think that ease of coding is more important than correctness and maintainability, does not mean the issue is moot, merely boring and unpopular.

I agree. Completely.
Can we add a page on the D Wiki for the "boolean != bit" party? This issue comes
back every one-two months, and always the same things are said about it. A big
summary of "bool != bit" advantages can be set up, so that the misbeliving can
be redirected there for more information.

I have another proposal:
All D standard libraries should use the standard alias "bool" every time a truth
value is handled. This will ease the bit -> bool conversion when is eventually
done.

Ciao


November 04, 2003
> I agree. Completely.

You are a man of singular wisdom. ;)