There are 127 messages in this thread.
You are currently looking at messages 80 to 90.
On 9 Nov 2006 06:29:20 -0800, "Eric" <e...@hotmail.com> wrote: >Any other ideas for interview questions? Ask to write code to toggle a variable from 0->1->0... We often saw this: if ( a == 0 ){ a=1;} else {a = 0;} instead of e.g a = 1-a; or a^= 1; Or ask them how to test a bit in a peripheral register. -- 42Bastian Do not email to b...@yahoo.com, it's a spam-only account :-) Use <same-name>@monlynx.de instead !
42Bastian Schick wrote:
> Ask to write code to toggle a variable from 0->1->0...
> We often saw this:
> if ( a == 0 ){ a=1;} else {a = 0;}
Which is a correct and proper way to do this.
if (a) a = 0;
else a = 1;
> instead of e.g
> a = 1-a; or a^= 1;
Dirty hacks. "Premature optimization is the root of all evil"
> Or ask them how to test a bit in a peripheral register.
(tst_bit(register, 3)
--
WBR, Yuriy.
"Resistance is futile"
On Thu, 16 Nov 2006 05:57:25 -0600, "Yuriy K." <y...@mail.ru> wrote: >42Bastian Schick wrote: > >> Ask to write code to toggle a variable from 0->1->0... >> We often saw this: ... >> instead of e.g >> a = 1-a; or a^= 1; > >Dirty hacks. "Premature optimization is the root of all evil" dirty hacks ? *ouch*, if this is already dirty I'll better don't let you see my sources :-) >> Or ask them how to test a bit in a peripheral register. > >(tst_bit(register, 3) :-) Next question: Compute the square root of an integer. Yuriy's answer: s = squareroot(a); -- 42Bastian Do not email to b...@yahoo.com, it's a spam-only account :-) Use <same-name>@monlynx.de instead !
In article <ejhjli$sp4$1...@news.netins.net>, "Yuriy K." <y...@mail.ru> writes: |> Dirty hacks. "Premature optimization is the root of all evil" Given the if/then construct, how should the compiler know that "a" can be *only* 0 or 1? Especially if "a" might be not initialized with a constant somewhere (where the compiler might be clever enough to figure it out) but rather read from some status register? So if the *programmer* knows that "a" is either 0 or 1, why shouldn't he write "a^=1;" (or "a^=SOME_MASK;" for the arbitrary case) right from the beginning but rather if/then? Rainer
Yuriy K. wrote:
> 42Bastian Schick wrote:
>
>> Ask to write code to toggle a variable from 0->1->0...
>> We often saw this:
>> if ( a == 0 ){ a=1;} else {a = 0;}
>
>
> Which is a correct and proper way to do this.
> if (a) a = 0;
> else a = 1;
No, it is not.
if(a != 0)
{
a = 0;
}
else
{
a = 1;
}
MISRA Rulez.
Alternative acceptable realization:
template <class T> toggle(T& a);
>
> Dirty hacks. "Premature optimization is the root of all evil"
You betcha.
>
>> Or ask them how to test a bit in a peripheral register.
>
>
> (tst_bit(register, 3)
Dirty hack.
There is no such things as bits and registers.
if(something_happened())
{
}
Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com
Vladimir Vassilevsky wrote:
> Yuriy K. wrote:
>
> > 42Bastian Schick wrote:
> >
> >> Ask to write code to toggle a variable from 0->1->0...
> >> We often saw this:
> >> if ( a == 0 ){ a=1;} else {a = 0;}
> >
> >
> > Which is a correct and proper way to do this.
> > if (a) a = 0;
> > else a = 1;
>
> No, it is not.
>
> if(a != 0)
> {
> a = 0;
> }
> else
> {
> a = 1;
> }
If it doesn't need to be optimized, I prefer this version:
a = !a;
"Vladimir Vassilevsky" <a...@hotmail.com> a écrit dans le message news: zq_6h.6360$I...@newssvr25.news.prodigy.net... > > > Yuriy K. wrote: > > > 42Bastian Schick wrote: > > > >> Ask to write code to toggle a variable from 0->1->0... > >> We often saw this: > >> if ( a == 0 ){ a=1;} else {a = 0;} > > > > > > Which is a correct and proper way to do this. > > if (a) a = 0; > > else a = 1; > > No, it is not. > > if(a != 0) > { > a = 0; > } > else > { > a = 1; > } > > MISRA Rulez. > > Alternative acceptable realization: > > template <class T> toggle(T& a); > > The only reasonable instruction is: a = ++a & 0x01 ; ;)
Lanarcam wrote: > The only reasonable instruction is: > a = ++a & 0x01 ; No, that one is undefined, because it has two side effects to 'a' and only one sequence point.
42Bastian Schick wrote: >>> Ask to write code to toggle a variable from 0->1->0... > ... >>> a = 1-a; or a^= 1; >> Dirty hacks. "Premature optimization is the root of all evil" > > dirty hacks ? Yes, they are. > *ouch*, if this is already dirty I'll better don't > let you see my sources :-) Sources like the one above are not maintainable. Support and modification two years later is a nightmare. >>> Or ask them how to test a bit in a peripheral register. >> (tst_bit(register, 3) > > :-) > > Next question: Compute the square root of an integer. > Yuriy's answer: s = squareroot(a); Actually, it is s = sqrt(a); C programmer shall know basic standard library functions. -- WBR, Yuriy. "Resistance is futile"
Rainer Buchty wrote: > |> Dirty hacks. "Premature optimization is the root of all evil" > > Given the if/then construct, how should the compiler know that "a" can be > *only* 0 or 1? Why should it care? > Especially if "a" might be not initialized with a constant > somewhere (where the compiler might be clever enough to figure it out) but > rather read from some status register? It does not matter. > So if the *programmer* knows that "a" is either 0 or 1, why shouldn't he > write "a^=1;" (or "a^=SOME_MASK;" for the arbitrary case) right from the > beginning but rather if/then? Because three years later new programmer will spend two hours trying to figure out if *a* can or can not be equal to 2 or 3, and what is this silly line meant to do. Unfortunately, a lot of "programmers" do not think about code clarity and maintainability. More often than not it is more important than saving two bytes and 500 nanoseconds. -- WBR, Yuriy. "Resistance is futile"