Re: Sort of Gray code to binary converter
- From: "Frank Bemelman" <f.bemelmanq@xxxxxxxxxxxxxxxxx>
- Date: Sun, 11 Dec 2005 21:17:47 +0100
"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)
.
- Follow-Ups:
- Re: Sort of Gray code to binary converter
- From: Fred Bloggs
- Re: Sort of Gray code to binary converter
- References:
- Sort of Gray code to binary converter
- From: John B
- Re: Sort of Gray code to binary converter
- From: Dave
- Re: Sort of Gray code to binary converter
- From: John B
- Re: Sort of Gray code to binary converter
- From: Frank Bemelman
- Re: Sort of Gray code to binary converter
- From: Spehro Pefhany
- Sort of Gray code to binary converter
- Prev by Date: Re: Thermocouple junctions
- Next by Date: Re: 250mA voltage clamp
- Previous by thread: Re: Sort of Gray code to binary converter
- Next by thread: Re: Sort of Gray code to binary converter
- Index(es):
Relevant Pages
|