View mode: basic / threaded / horizontal-split · Log in · Help
June 20, 2005
error with "bit" data type
Hi *,

The following code compiles w/o any problem, but the results are, IMHO, wrong.
Here goes the code:

--- t1.d ---
import std.c.stdio;

int main( char [][] argv ){
bit  b1, b2;

b1 = b2 = 1;
printf("%d\n",b1+b2);
printf("%d\n",cast(bit)(b1+b2));

return 0;
}
------

The output is:
2
1

For the first case, I would expect that two bits added would produce also a bit,
but (perhaps) internally they are being converted into ints, added, and then the
resulting value is printf'ed. But, for me, even worse is the following line,
where a type cast is used, and the result is 1.
In bit-algebra, or Galois Field 2, 1+1=0, not 1!!!
Am I missing something?

BR,
Tiago
June 20, 2005
Re: error with "bit" data type
tiago.gasiba@gmail.com wrote:

> For the first case, I would expect that two bits added would produce also a bit,
> but (perhaps) internally they are being converted into ints, added, and then the
> resulting value is printf'ed.

Correct, it is silently undergoing "integral promotion" as per the spec.

> But, for me, even worse is the following line,
> where a type cast is used, and the result is 1.
> In bit-algebra, or Galois Field 2, 1+1=0, not 1!!!
> Am I missing something?

Yes, the D "bit" type is a boolean type. It's not very useful for bits ?


It would probably be better for everyone if it ("bit") would just die...
(new name for it being "bool", which is currently implemented as alias)

But I think it'll take a few years more before Walter kills his darling.

--anders
June 20, 2005
Re: error with "bit" data type
"Anders F Björklund" <afb@algonet.se> wrote in message 
news:d96bsr$p7f$1@digitaldaemon.com...
> tiago.gasiba@gmail.com wrote:
>
>> For the first case, I would expect that two bits added would produce also 
>> a bit,
>> but (perhaps) internally they are being converted into ints, added, and 
>> then the
>> resulting value is printf'ed.
>
> Correct, it is silently undergoing "integral promotion" as per the spec.
>
>> But, for me, even worse is the following line,
>> where a type cast is used, and the result is 1.
>> In bit-algebra, or Galois Field 2, 1+1=0, not 1!!!
>> Am I missing something?
>
> Yes, the D "bit" type is a boolean type. It's not very useful for bits ?
>
>
> It would probably be better for everyone if it ("bit") would just die...
> (new name for it being "bool", which is currently implemented as alias)
>
> But I think it'll take a few years more before Walter kills his darling.

:))

I think in most cases it is enough to enable
opIndex & co. for integer types.

int w = 1;
if( w[0] ) .... // retireve first bit of the int.

This a) could be implemented effectively and b) allows to keep binary 
compatibility
with legacy C code.

Andrew.
June 25, 2005
Re: error with "bit" data type
<tiago.gasiba@gmail.com> wrote in message
news:d965g7$lbd$1@digitaldaemon.com...
> --- t1.d ---
> import std.c.stdio;
>
> int main( char [][] argv ){
> bit  b1, b2;
>
> b1 = b2 = 1;
> printf("%d\n",b1+b2);
> printf("%d\n",cast(bit)(b1+b2));
>
> return 0;
> }
> ------
>
> The output is:
> 2
> 1
>
> For the first case, I would expect that two bits added would produce also
a bit,
> but (perhaps) internally they are being converted into ints, added, and
then the
> resulting value is printf'ed. But, for me, even worse is the following
line,
> where a type cast is used, and the result is 1.
> In bit-algebra, or Galois Field 2, 1+1=0, not 1!!!
> Am I missing something?

cast(bit)(b1 + b2) is evaluated as if it were:

   cast(bit)(cast(int)b1 + cast(int)b2)

which is:

   cast(bit)( 2 )

Casting an int to bit is done as:

   cast(bit)(i) => (i != 0) ? 1 : 0

To do bit ANDing, use the & operator rather than the + operator.
Top | Discussion index | About this forum | D home