Re: Sort of Gray code to binary converter



"Spehro Pefhany" <speffSNIP@xxxxxxxxxxxxxxxxxxxxxxx> schreef in bericht
news:pvsop19okbua2anekr11fa3mvgnaqthq5v@xxxxxxxxxx
> On Sun, 11 Dec 2005 19:27:59 +0100, the renowned "Frank Bemelman"
> <f.bemelmanq@xxxxxxxxxxxxxxxxx> wrote:
>
> >"John B" <spamj_baraclough@xxxxxxxxxxxxxxxxxxx> schreef in bericht
> >news:439c5a1f$0$12562$4c56ba96@xxxxxxxxxxxxxxxxxxxxxxxxx
> >> On 11/12/2005 the venerable Dave etched in runes:
> >>
> >> <snip>
> >>
> >> > Mostly depends on what else you want to do with the number when
> >> > you've got it !
> >> >
> >> > Dave
> >>
> >> I want to use it as an index into a look-up table of wind directions in
> >> degrees from North. What I have just now is very ugly and I don't like
> >> it.
> >>
> >> if(code == 0x01) Direction = 0;
> >> else if(code == 0x03) Direction = 45;
> >> else if(code == 0x02) Direction = 90;
> >
> >That is not so bad at all. It looks perhaps bad, but it isn't.
> >It's the same as a switch statement.
>
> Probably/possibly it is in this situation, but there are other things
> a compiler can do with a switch/case construct than a simple series of
> comparisons (linear search). Binary tree, table, hash, etc.

Right. If a large enough switch statement where an 8 bit switch argument
is used, a binary tree comes to mind, but with smaller switches it all
boils down to series of if/else cases. Writing it in if/else form, it
allows you to optimize for the situation at hand, put the most likely
possibilities at the top, things like that. This is all very understandable,
as there exists no assembly equivalent for a switch statement, so any
cleverness, if any, has to come from the compiler if you depend on the
switch statement. I have yet to see a compiler that uses anything else
than if/else approach, but perhaps I haven't used large enough switches
to see a different behaviour.

--
Thanks, Frank.
(remove 'q' and '.invalid' when replying by email)






.



Relevant Pages

  • Re: Has thought been given given to a cleaned up C? Possibly called C+.
    ... leave the switch statement alone and add a new form of selection ... The advantage is you don't need a goto label. ... compiler being used in 1980 in which ... they aren't keywords. ...
    (comp.lang.c)
  • Re: hi i am a girl who were trying a bit on C can someone help?
    ... You may want to take a look at the compiler warnings that get issued. ... your code, with those tabs set at ... >empty, empty); ... You may want to convert the if/else ladder into a switch statement. ...
    (comp.lang.c)
  • Re: Handling 5000 different functionalities - Optimised way in C
    ... the jump table that's typically used to implement a switch statement. ... Perhaps that's not technically a jump table, but AFAIK it's the closest thing* one can implement in C, therefore the name is descriptive enough. ... I agree that the compiler _could_ generate something faster, by jumping to code inside the same function instead of calling another function, but that shouldn't be a noticeable difference. ... that assumes the table is sorted, which may not always be true, my first attempt was obviously wrong and I decided getting the point across was more important than figuring out how to code a binary search on a non-power-of-two-sized array, and a binary search may have a larger I-cache footprint, bomb the branch predictor, and/or confuse the data prefetcher.) ...
    (comp.lang.c)
  • Re: [OT] Switch question
    ... On Sat, 21 Feb 2004, Sean Kenwrick wrote: ... > a byte code interpreter I am working on, and I am wondering if I should take ... Each compiler can do it differently. ... If a switch statement used branching (a parallel ...
    (comp.lang.c)
  • Re: why doesnt this work? final assignments and switches
    ... whatever, however, it seems that the compiler only performs a very ... This should be sufficient to work a switch statement - I believe. ... Sure, there are work arounds, but since java offered modest developer ... conveniences to sdk1.5 - how about this one which helps the "hand coder". ...
    (comp.lang.java.programmer)