Sign in

username:

password:



Not a member?

Search Comp.Arch.Embedded



Search tips

embedded by Keywords

68HC11 | 68HC12 | 8051 | 8052 | ARM | ARM7 | Asic | AT91 | AT91RM9200 | Atmel | AVR | AVRStudio | Bootloader | CFP | CompactFlash | Cygnal | Cypress | Dataflash | DSP | eCos | EEPROM | Embedded Linux | Emulator | Endian | Ethernet | Firewire | FPGA | Freescale | GCC | GNUARM | GSM | H8 | HDLC | I2C | Infineon | Interrupts | Java | JTAG | LCD | LED | LPC2000 | MCU | Microchip | MMC | MPLAB | MSP430 | PC104 | PCB | PCI | PCMCIA | PowerPC | Rabbit | RS232 | RS485 | RTOS | SBC | SDRAM | Sensor | SPI | STK500 | UART | UML | USART | USB | Verilog | VHDL | VxWorks | Xilinx

Ads

Discussion Groups

Discussion Groups | Comp.Arch.Embedded | Interview Embedded Software Questions

There are 127 messages in this thread.

You are currently looking at messages 80 to 90.

Re: Interview Embedded Software Questions - 42Bastian Schick - 02:54 16-11-06

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 !



Re: Interview Embedded Software Questions - Yuriy K. - 06:57 16-11-06

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"

Re: Interview Embedded Software Questions - 42Bastian Schick - 07:38 16-11-06

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 !

Re: Interview Embedded Software Questions - Rainer Buchty - 09:06 16-11-06

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

Re: Interview Embedded Software Questions - Vladimir Vassilevsky - 09:18 16-11-06


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

Re: Interview Embedded Software Questions - Arlet - 10:45 16-11-06

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;


Re: Interview Embedded Software Questions - Lanarcam - 10:50 16-11-06

"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 ;

;)



Re: Interview Embedded Software Questions - Arlet - 11:08 16-11-06

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.


Re: Interview Embedded Software Questions - Yuriy K. - 13:22 16-11-06

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"

Re: Interview Embedded Software Questions - Yuriy K. - 13:28 16-11-06

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"

previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | next