Is there ANY chance we can fix the bitwise operator precedence rules?

bearophile bearophileHUGS at lycos.com
Fri Jun 18 18:36:31 PDT 2010


Jonathan M Davis:

>Well, I would pount out that you mentioning it more or less reopens the discussion,<

You are right, but probably Walter will not sue me for reopening an old thread ;-)


In the things you are saying you seem to ignore the "goto" I have written two times in my answers :-)


>An extension of that is if you have two cases which are almost identical but where one of them needs to do something first before the code that is common between both cases.<

You can write this C-style switch:

switch (x) {
    case 0: foo();
    case 1: bar();
}

As this (this is D syntax that you can already use):

void main() {
    int x, y;
    switch (x) {
        case 0: y++; goto case 1;
        case 1: y++;
        default:
    }
}


> A more complicated example would be one where you're doing something like
> Duff's Device:
> 
> send(to, from, count)
> register short *to, *from;
> register count;
> {
>         register n=(count+7)/8;
>         switch(count%8){
>         case 0: do{     *to = *from++;
>         case 7:         *to = *from++;
>         case 6:         *to = *from++;
>         case 5:         *to = *from++;
>         case 4:         *to = *from++;
>         case 3:         *to = *from++;
>         case 2:         *to = *from++;
>         case 1:         *to = *from++;
>                 }while(--n>0);
>         }
> }


You can use gotos (this is a different function, it copies whole arrays, 'to' too is incremented):

import std.stdio: writeln;

void arrryCopy(short* to, short* from, int count) {
    foreach (_; 0 .. count / 8) {
        *to++ = *from++; *to++ = *from++;
        *to++ = *from++; *to++ = *from++;
        *to++ = *from++; *to++ = *from++;
        *to++ = *from++; *to++ = *from++;
    }

    final switch (count % 8) {
        case 7: *to++ = *from++; goto case 6;
        case 6: *to++ = *from++; goto case 5;
        case 5: *to++ = *from++; goto case 4;
        case 4: *to++ = *from++; goto case 3;
        case 3: *to++ = *from++; goto case 2;
        case 2: *to++ = *from++; goto case 1;
        case 1: *to++ = *from++; break;
        case 0: break;
    }
}

void main() {
    short[9] a1 = 1;
    short[a1.length] a2;
    writeln(a1, "   ", a2);

    arrryCopy(a2.ptr, a1.ptr, a1.length);
    writeln(a1, "   ", a2);
}


I have used the static switch to avoid the default case.
Using gotos like that is a bit less conventient than the C code, but I think it can be acceptable.

> switch(value)
> {
>     case 0:
>         do something...
>     case 1:
>         do something else...
>     case 2:
>         do a third thing...
>     case 3:
>         do yet more...
> }


You can translate it as:

switch(value)
{
    case 0:
        do something...
        goto case 1;
    case 1:
        do something else...
        goto case 2;
    case 2:
        do a third thing...
        goto case 3;
    case 3:
        do yet more...
        break;
    default:
        ...
}

Bye,
bearophile


More information about the Digitalmars-d mailing list