Explore topic-wise InterviewSolutions in Current Affairs.

This section includes 7 InterviewSolutions, each offering curated multiple-choice questions to sharpen your Current Affairs knowledge and support exam preparation. Choose a topic below to get started.

1.

What Is Concurrency? Explain With Example Deadlock And Starvation.

Answer»

Concurrency is nothing but execution of different transactions simultaneously on one single resource.
Dead lock :
A group of threads are waiting for resources held by others in the group. None of them will ever make progress.
Example :
An example of a deadlock which may occur in database products is the following. Client applications using the database may require exclusive access to a table, and in order to GAIN exclusive access they ask for a lock. If one client application holds a lock on a table and attempts to OBTAIN the lock on a second table that is ALREADY held by a second client application, this may lead to deadlock if the second application then attempts to obtain the lock that is held by the first application.
Starvation :
A thread may wait indefinitely because other threads keep coming in and getting the requested resources before this thread does. Note that resource is being actively USED and the thread will stop waiting if other threads stop coming in.
Example :
High PRIORITY thread:

while(1);

if the system is priority based system then low priority thread never gets a chance.

 

Concurrency is nothing but execution of different transactions simultaneously on one single resource.
Dead lock :
A group of threads are waiting for resources held by others in the group. None of them will ever make progress.
Example :
An example of a deadlock which may occur in database products is the following. Client applications using the database may require exclusive access to a table, and in order to gain exclusive access they ask for a lock. If one client application holds a lock on a table and attempts to obtain the lock on a second table that is already held by a second client application, this may lead to deadlock if the second application then attempts to obtain the lock that is held by the first application.
Starvation :
A thread may wait indefinitely because other threads keep coming in and getting the requested resources before this thread does. Note that resource is being actively used and the thread will stop waiting if other threads stop coming in.
Example :
High priority thread:

if the system is priority based system then low priority thread never gets a chance.

 

2.

Explain The Working Of Virtual Memory?

Answer»

Virtual memory is a computer system technique which gives an application program the impression that it has contiguous working memory (an address space), while in FACT it may be physically fragmented and may even overflow on to DISK storage. Systems that use this technique make programming of large applications easier and use REAL physical memory (e.g. RAM) more EFFICIENTLY than those without virtual memory.

Virtual memory is a computer system technique which gives an application program the impression that it has contiguous working memory (an address space), while in fact it may be physically fragmented and may even overflow on to disk storage. Systems that use this technique make programming of large applications easier and use real physical memory (e.g. RAM) more efficiently than those without virtual memory.

3.

What Is A Inode?

Answer»

In computing, an inode is a DATA structure on a TRADITIONAL Unix-style file SYSTEM such as UFS. An inode stores BASIC information about a regular file, DIRECTORY, or other file system object.

In computing, an inode is a data structure on a traditional Unix-style file system such as UFS. An inode stores basic information about a regular file, directory, or other file system object.

4.

Scope Of Static Variables?

Answer»

Scope of static VARIABLE is WITHIN the file if it is static GLOBAL. Scope of static variable is within the function if variable is declared local to a function. But the life time is THROUGHOUT the program.

Scope of static variable is within the file if it is static global. Scope of static variable is within the function if variable is declared local to a function. But the life time is throughout the program.

5.

What Happens When Recursion Functions Are Declared Inline?

Answer»

It is ILLEGAL to declare a recursive function as inline. Even a function is DECLARED as inline compiler judges it to be inline or not. Many compilers can also inline EXPAND some recursive functions; recursive macros are typically illegal.

It is illegal to declare a recursive function as inline. Even a function is declared as inline compiler judges it to be inline or not. Many compilers can also inline expand some recursive functions; recursive macros are typically illegal.

6.

Difference In Inline Functions And Macro

Answer»
  •  MACRO is expanded by preprocessor and inline FUNCTION are expanded by COMPILER.
  •  Expressions passed as arguments to inline functions are evaluated only once while _expression passed as argument to inline functions are evaluated more than once.
  • More over inline functions are used to overcome the overhead of function calls.
  • MACROS are used to maintain the readability and easy maintenance of the code.

7.

What Is Mutex?

Answer»

Mutual exclusion (often abbreviated to mutex) algorithms are USED in CONCURRENT PROGRAMMING to avoid the simultaneous use of a common resource, such as a global variable, by pieces of COMPUTER code called critical SECTIONS.

Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent programming to avoid the simultaneous use of a common resource, such as a global variable, by pieces of computer code called critical sections.

8.

What Is Semaphore?

Answer»

In computer science, a semaphore is a protected variable or abstract data type which constitutes the CLASSIC method for restricting access to shared resources such as shared memory in a parallel programming environment. A counting semaphore is a counter for a set of AVAILABLE resources, rather than a locked/unlocked flag of a SINGLE RESOURCE.

In computer science, a semaphore is a protected variable or abstract data type which constitutes the classic method for restricting access to shared resources such as shared memory in a parallel programming environment. A counting semaphore is a counter for a set of available resources, rather than a locked/unlocked flag of a single resource.

9.

What Is Watchdog Timer?

Answer»

A watchdog TIMER (or computer operating properly timer) is a computer HARDWARE timing device that triggers a system reset if the main program, due to some fault CONDITION, such as a hang, NEGLECTS to REGULARLY service the watchdog. The intention is to bring the system back from the hung state into normal operation.

A watchdog timer (or computer operating properly timer) is a computer hardware timing device that triggers a system reset if the main program, due to some fault condition, such as a hang, neglects to regularly service the watchdog. The intention is to bring the system back from the hung state into normal operation.

10.

Explain Can Microcontroller Work Independently?

Answer»

Obviously, it can work independently. But to see the output we need certain output devices like LED, Buzzer can be CONNECTED to check its functionality. Without the HELP of any o/p DEVICE connected we can check the functionality of Microcontroller.

Obviously, it can work independently. But to see the output we need certain output devices like LED, Buzzer can be connected to check its functionality. Without the help of any o/p device connected we can check the functionality of Microcontroller.

11.

While Writing Interrupt Handlers (isr), Which Are Points Needed To Be Considered?

Answer»

AVOID SLEEP, USE GFP_ATOMIC INSTEAD of GFP_KERNEL in kmalloc.

Avoid sleep, use GFP_ATOMIC instead of GFP_KERNEL in kmalloc.

12.

What Is The Difference Between Testing And Verification Of Vlsi Circuit?

Answer»
  • Verification is a FRONT end PROCESS and testing is a post silicon process.
  • Verification is to VERIFY the functionality of the design during the design cycle.
  • Testing is finding manufacturing FAULTS.

13.

What Is Dirac Delta Function And Its Fourier Transform And Its Importance?

Answer»
  • Dirac delta is a continuous time function with unit AREA and infinite amplitude at t=0 the fourier transform of dirac delta is 1.
  • using dirac delta as an INPUT to the system, we can get the system response. it is used to study the BEHAVIOR of the circuit.
  • we can use this system behavior to find the OUTPUT for any input.

14.

Dma Deals With Which Address (physical/virtual Addresses)?

Answer»

DMA deals with Physical addresses.

  • Only when CPU accesses addresses it refers to MMU(MEMORY Management Unit) and MMU converts the Physical address to Virtual address.
  • But, DMA controller is a DEVICE which directly DRIVES the data and address bus during data transfer. So, it is purely Physical address. (It never NEEDS to GO through MMU & Virtual addresses).
  • That is why when writing the device drivers, the physical address of the data buffer has to be assigned to the DMA.

DMA deals with Physical addresses.

15.

Can We Use Semaphore Or Mutex Or Spin Lock In Interrupt Context In Linux Kernel?

Answer»

We cannot sleep in interrupt CONTEXT so semaphores and mutex can't be USED. SPINLOCKS can be used for LOCKING in interrupt context.

We cannot sleep in interrupt context so semaphores and mutex can't be used. Spinlocks can be used for locking in interrupt context.

16.

What Is Difference Between Micro Processor & Micro Controller?

Answer»
  •  Microprocessor is a manager of the RESOURCES (I/O, Memory) which lie out-side of its architecture.
  • Micro-controllers have I/O, Memory etc. built into it and SPECIALLY designed for CONTROL applications.

17.

What Is Plc System?

Answer»

PROGRAMMING LOGICAL CONTROL SYSTEM.

Programming logical control system.

18.

What Type Of Registers Contains An (intel) Cpu?

Answer»

Special function registers like accumulator, Program CONTROLLER(PC),data POINTER(DPTR),TMOD and TCON (TIMING registers),3 REGISTER banks with r0 to R7,Bit addressable registers like B.

Special function registers like accumulator, Program controller(PC),data pointer(DPTR),TMOD and TCON (timing registers),3 register banks with r0 to r7,Bit addressable registers like B.

19.

What Is The Role Of Segment Register?

Answer»

In the x86 processor architecture, memory addresses are specified in two PARTS called the segment and the offset. One usually thinks of the segment as specifying the beginning of a block of memory allocated by the system and the offset as an index into it. Segment values are stored in the segment REGISTERS. There are four or more segment registers: CS contains the segment of the current instruction (IP is the offset), SS contains the stack segment (SP is the offset), DS is the segment used by default for most data operations, ES (and, in more RECENT processors, FS and GS) is an extra segment register. Most memory operations ACCEPT a segment override prefix that allows use of a segment register other than the default one.

In the x86 processor architecture, memory addresses are specified in two parts called the segment and the offset. One usually thinks of the segment as specifying the beginning of a block of memory allocated by the system and the offset as an index into it. Segment values are stored in the segment registers. There are four or more segment registers: CS contains the segment of the current instruction (IP is the offset), SS contains the stack segment (SP is the offset), DS is the segment used by default for most data operations, ES (and, in more recent processors, FS and GS) is an extra segment register. Most memory operations accept a segment override prefix that allows use of a segment register other than the default one.

20.

Is 8085 An Embedded System?

Answer»

its not a EMBEDDED SYSTEM...because it will be a PART of a embedded system and it does not WORK on any SOFTWARE.

its not a embedded system...because it will be a part of a embedded system and it does not work on any software.

21.

How To Implement A Fourth Order Butterworth Lp Filter At 1khz If Sampling Frequency Is 8 Khz?

Answer»

A FOURTH order Butterworth filter can be made as CASCADE of two second order LP filters with zeta of 0.924 and 0.383. ONE can use a bilinear transformation approach for REALIZING second order LP filters. Using this technique described WELL in many texts, one can make two second order LP filters and cascade them.

A fourth order Butterworth filter can be made as cascade of two second order LP filters with zeta of 0.924 and 0.383. One can use a bilinear transformation approach for realizing second order LP filters. Using this technique described well in many texts, one can make two second order LP filters and cascade them.

22.

What Is An Anti Aliasing Filter? Why Is It Required?

Answer»

Anti aliasing filter reduces errors due to aliasing. If a SIGNAL is sampled at 8 kS/S, the max frequency of the input should be 4 kHz. Otherwise, aliasing errors will result. Typically a 3.4kHz will have an image of 4.6 khz, and one uses a sharp cut off filter with gain of about 1 at 3.4kHz and gain of about 0.01 at 4.6 kHz to effectively guard against aliasing. THUS one does not quite choose max frequency as simply fs/2 where fs is sampling frequency. One has to have a guard BAND of about 10% of this fmax, and CHOOSES max signal frequency as 0.9*fs/2.

Anti aliasing filter reduces errors due to aliasing. If a signal is sampled at 8 kS/S, the max frequency of the input should be 4 kHz. Otherwise, aliasing errors will result. Typically a 3.4kHz will have an image of 4.6 khz, and one uses a sharp cut off filter with gain of about 1 at 3.4kHz and gain of about 0.01 at 4.6 kHz to effectively guard against aliasing. Thus one does not quite choose max frequency as simply fs/2 where fs is sampling frequency. One has to have a guard band of about 10% of this fmax, and chooses max signal frequency as 0.9*fs/2.

23.

Is It Necessary To Start The Execution Of A Program From The Main() In C?

Answer»

"Normally you are at LIBERTY to give functions WHATEVER names you like, but "main"' is SPECIAL - your program begins executing at the beginning of main. This means that every program must have a main SOMEWHERE." 

"Normally you are at liberty to give functions whatever names you like, but "main"' is special - your program begins executing at the beginning of main. This means that every program must have a main somewhere." 

24.

What Is The Difference Between Fifo And The Memory?

Answer»

Fifo(First In Last Out) is a MEMORY structure where data can be STORED and retrieved (in the order of its entry only). This is a queue,wheras Memory is a STORAGE device which can HOLD data dynamically or at any DESIRED locations and can be retrieved in any order.

Fifo(First In Last Out) is a memory structure where data can be stored and retrieved (in the order of its entry only). This is a queue,wheras Memory is a storage device which can hold data dynamically or at any desired locations and can be retrieved in any order.

25.

How To Define A Structure With Bit Field Members?

Answer»

You can define structure bit field members with Dot OPERATORS.

EXAMPLE:

#include <stdio.h&GT; INT main() { Struct bit_field { Int X.4; // it allocates only 4 bits to x Char C.6; // it allocates only 6 bits to C; }; return 0; }

You can define structure bit field members with Dot operators.

EXAMPLE:

26.

How Is Function Itoa() Written In C?

Answer»

Answer : #include<stdlib.h> #include<stdio.h> INT MAIN() { int n = 6789; CHAR p[20]; ITOA(n,s,10); printf("n=%d,s=%s",n,s); return 0; }

27.

Explain What Is Forward Reference W.r.t. Pointers In C?

Answer»

<P>POINTER use's to REFERENCE to value a into int a=10 to memory add this value and 10 is add p value ADDED this DATA in memory location for p.

Pointer use's to reference to value a into int a=10 to memory add this value and 10 is add p value added this data in memory location for p.

28.

Explain What Are The Different Storage Classes In C?

Answer»

FOUR types of storage classes are there in C.
1.Auto
2.Register
3.Static
4.Extern or GLOBAL

Four types of storage classes are there in c.
1.Auto
2.Register
3.Static
4.Extern or Global

29.

Explain Can We Have Constant Volatile Variable?

Answer»

Const and volatile KEYWORDS should not be USED TOGETHER because both are opposite in nature. A VARIABLE is DECLARED as "const" means it's value is not able to be changed but if it is declared as "Volatile" then it is not under control.

Const and volatile keywords should not be used together because both are opposite in nature. A variable is declared as "const" means it's value is not able to be changed but if it is declared as "Volatile" then it is not under control.

30.

Explain Can Structures Be Passed To The Functions By Value?

Answer»

Yes structures can be passed to functions by value. Though PASSING by value has two disadvantages:
1) The CHARGES by the calling function are not reflected.
2) It’s slower than the PASS by REFERENCE function call.

Yes structures can be passed to functions by value. Though passing by value has two disadvantages:
1) The charges by the calling function are not reflected.
2) It’s slower than the pass by reference function call.

31.

Explain What Will This Return Malloc(sizeof(-10))?

Answer»

It will return a 4 byte ADDRESS value.
Because -10 is a signed integer(VARIES from COMPILER to compiler).

It will return a 4 byte address value.
Because -10 is a signed integer(varies from compiler to compiler).

32.

Explain What Are The 5 Different Types Of Inheritance Relationship?

Answer»

5 level types are as under:

  • single: B DERIVED from A.
  • MULTILEVEL: derived from B and B derived from A.
  • multiple: C derived from A and B.
  • Hierarchical: B derived from A and C derived from A.
  • HYBRID: combination of above types.

5 level types are as under:

33.

Explain What Are The Different Qualifiers In C?

Answer»

1) VOLATILE:
A variable should be declared volatile whenever its value could change unexpectedly. In practice, only three types of variables could change:
► Memory-mapped peripheral registers
► Global variables modified by an interrupt service routine
► Global variables WITHIN a multi-threaded application

2) Constant:
The addition of a 'const' qualifier indicates that the (relevant PART of the) PROGRAM may not modify the variable.

1) Volatile:
A variable should be declared volatile whenever its value could change unexpectedly. In practice, only three types of variables could change:
► Memory-mapped peripheral registers
► Global variables modified by an interrupt service routine
► Global variables within a multi-threaded application

2) Constant:
The addition of a 'const' qualifier indicates that the (relevant part of the) program may not modify the variable.

34.

Explain What Is Interrupt Latency? How Can We Reduce It?

Answer»

INTERRUPT LATENCY is the TIME required to RETURN from the interrupt service routine after tackling a particular interrupt. We can reduce it by writing smaller ISR routines.

Interrupt latency is the time required to return from the interrupt service routine after tackling a particular interrupt. We can reduce it by writing smaller ISR routines.

35.

Explain Why Cannot Arrays Be Passed By Values To Functions?

Answer»

Because in C when you say the name of the array it means the address of the first element.
example :

INT a[]; FUNC (a); int func(int a[]);

In this when you call the function by passing the argument a actually &AMP;a[0](address of first element) gets passed. Hence it is IMPOSSIBLE to pass by value in C.

 

Because in C when you say the name of the array it means the address of the first element.
example :

In this when you call the function by passing the argument a actually &a[0](address of first element) gets passed. Hence it is impossible to pass by value in C.

 

36.

What Are The Advantages And Disadvantages Of Using Macro And Inline Functions?

Answer»

Advantage:
MACROS and Inline functions are efficient than calling a normal function. The times spend in calling the function is SAVED in case of macros and inline functions as these are INCLUDED DIRECTLY into the code.
Disadvantage:
Macros and inline functions increased the size of EXECUTABLE code.

Advantage:
Macros and Inline functions are efficient than calling a normal function. The times spend in calling the function is saved in case of macros and inline functions as these are included directly into the code.
Disadvantage:
Macros and inline functions increased the size of executable code.

37.

Explain Difference Between Object Oriented And Object Based Languages?

Answer»

Object based languages doesn’t support Inheritance where as object ORIENTED supports. c# is a object oriented language because it supports inheritance and asp.net is not a language it is a technology. If the language supports only 3 features i;e (data ENCAPSULATION, data ABSTRACTION and polymorphism).then it is said to be object based programming language. If the language supports all the 4 features i;e (encapsulation, abstraction, polymorphism and also inheritance ) then it is said to be object oriented programming language.

Object based languages doesn’t support Inheritance where as object oriented supports. c# is a object oriented language because it supports inheritance and asp.net is not a language it is a technology. If the language supports only 3 features i;e (data encapsulation, data abstraction and polymorphism).then it is said to be object based programming language. If the language supports all the 4 features i;e (encapsulation, abstraction, polymorphism and also inheritance ) then it is said to be object oriented programming language.

38.

Explain Order Of Constructor And Destructor Call In Case Of Multiple Inheritance?

Answer»

constructors top-down, destructors bottom-up.
EG:

  • in parent's constructor
  • in CHILD's constructor
  • in grandchild's constructor
  • in grandchild's DESTRUCTOR
  • in child's destructor
  • in parent's destructor

constructors top-down, destructors bottom-up.
eg:

39.

Explain Scope Of Static Variables?

Answer»
  • STATIC variables can only be ACCESSED in the files were they are DECLARED.
  • Static VARIABLE within the scope of a function store it's values in consecutive calls of that function.
  • Static functions can only be called within the file they are defined.

40.

Explain What Happens When Recursion Functions Are Declared Inline?

Answer»

Inline functions property says whenever it will called, it will copy the complete DEFINITION of that function. RECURSIVE function declared as inline creates the BURDEN on the COMPILERS execution. The SIZE of the stack may/may not be overflow if the function size is big.

Inline functions property says whenever it will called, it will copy the complete definition of that function. Recursive function declared as inline creates the burden on the compilers execution. The size of the stack may/may not be overflow if the function size is big.

41.

Accessing Fixed Memory Locations?

Answer»

Embedded systems are OFTEN characterized by requiring the programmer to access a specific memory LOCATION. On a certain project it is required to set an integer variable at the absolute address 0x67a9 to the value 0xaa55. The compiler is a pure ANSI compiler. Write code to accomplish this task.

This problem TESTS whether you know that it is legal to typecast an integer to a pointer in order to access an absolute location. The exact syntax varies depending upon one's STYLE. However, I would typically be looking for something like this:

int *ptr; ptr = (int *)0x67a9; *ptr = 0xaa55;

A more OBSCURE approach is:

*(int * const)(0x67a9) = 0xaa55;

Embedded systems are often characterized by requiring the programmer to access a specific memory location. On a certain project it is required to set an integer variable at the absolute address 0x67a9 to the value 0xaa55. The compiler is a pure ANSI compiler. Write code to accomplish this task.

This problem tests whether you know that it is legal to typecast an integer to a pointer in order to access an absolute location. The exact syntax varies depending upon one's style. However, I would typically be looking for something like this:

A more obscure approach is:

42.

Data Declarations?

Answer»

Examples of DATA declarations:

a) INT a;
An integer

b) int *a;
A pointer to an integer

C) int **a;
A pointer to a pointer to an integer

d) int a[10];
An array of 10 INTEGERS

e) int *a[10];
An array of 10 pointers to integers

f) int (*a)[10];
A pointer to an array of 10 integers

g) int (*a)(int);
A pointer to a function a that TAKES an integer argument and returns an integer

h) int (*a[10])(int);
An array of 10 pointers to functions that take an integer argument and return an integer

Examples of data declarations:

a) int a;
An integer

b) int *a;
A pointer to an integer

c) int **a;
A pointer to a pointer to an integer

d) int a[10];
An array of 10 integers

e) int *a[10];
An array of 10 pointers to integers

f) int (*a)[10];
A pointer to an array of 10 integers

g) int (*a)(int);
A pointer to a function a that takes an integer argument and returns an integer

h) int (*a[10])(int);
An array of 10 pointers to functions that take an integer argument and return an integer

43.

Infinite Loops Often Arise In Embedded Systems. How Does You Code An Infinite Loop In C?

Answer»

There are SEVERAL solutions to this question. One is,

while(1) { }

Other is using for loop, but here things are not PRETTY clear as to what is going on.

for(;;) { }

Even though both serve the same purpose, its always better to KNOW why you are using the first or second. So when you are asked about such questions you must answer with confidence that both are right, and just matter which WAY you wish to code it, anyways finally compiler would optimize and generate the same code for it. So don’t ever say, I was taught to do it this way, and so never thought about the other ways.
There is another way of doing it, i.e. by using goto statement, goto is very basic keyword, which is more like an assembly jump instruction, if one FINDS more comfortable with goto then possibly he works closely with assembly language, or with FORATN.

loop: ... ... goto loop;

There are several solutions to this question. One is,

Other is using for loop, but here things are not pretty clear as to what is going on.

Even though both serve the same purpose, its always better to know why you are using the first or second. So when you are asked about such questions you must answer with confidence that both are right, and just matter which way you wish to code it, anyways finally compiler would optimize and generate the same code for it. So don’t ever say, I was taught to do it this way, and so never thought about the other ways.
There is another way of doing it, i.e. by using goto statement, goto is very basic keyword, which is more like an assembly jump instruction, if one finds more comfortable with goto then possibly he works closely with assembly language, or with FORATN.

44.

What Is A Pure Function In Arm Terminology?

Answer»

Pure functions are those which RETURN a result which depends only on their arguments. They can be THOUGHT of as mathematical functions: they always return the same result if the arguments are the same. To tell the compiler that a function is pure, use the special DECLARATION keyword __pure.

__pure int square(int X) { return x * x; }

Pure functions are those which return a result which depends only on their arguments. They can be thought of as mathematical functions: they always return the same result if the arguments are the same. To tell the compiler that a function is pure, use the special declaration keyword __pure.

45.

How To Reduce Function Call Overhead In Arm Based Systems

Answer»
  •  TRY to ensure that small functions TAKE four or fewer arguments. These will not use the stack for argument passing. It will copy into registers.
  •  If a function needs more than four arguments, try to ensure that it does a SIGNIFICANT amount of work, so that the cost of passing the stacked arguments is outweighed.
  •  Pass pointers to structures instead of passing the structure itself.
  •  Put related arguments in a structure, and pass a pointer to the structure to functions. This will reduce the number of parameters and increase readability.
  •  Minimize the number of long long parameters, as these take two argument words. This also applies to doubles if software floating-point is enabled.
  •  Avoid functions with a parameter that is passed PARTIALLY in a register and partially on the stack (split-argument). This is not handled efficiently by the CURRENT compilers: all register arguments are pushed on the stack.
  •  Avoid functions with a variable number of parameters. Varargs functions.

46.

Which Is Better A Char, Short Or Int Type For Optimization?

Answer»

Where possible, it is best to avoid using char and short as local variables. For the types char and short the compiler needs to reduce the size of the local variable to 8 or 16 bits after each assignment. This is called sign-extending for SIGNED variables and zero extending for unsigned variables. It is implemented by shifting the register left by 24 or 16 bits, FOLLOWED by a signed or unsigned shift right by the same AMOUNT, taking two instructions (zero-extension of an unsigned char takes one instruction).

These shifts can be avoided by using int and unsigned int for local variables. This is particularly important for calculations which first load data into local variables and then process the data inside the local variables. EVEN if data is input and output as 8- or 16-bit quantities, it is worth considering processing them as 32bit quantities

Where possible, it is best to avoid using char and short as local variables. For the types char and short the compiler needs to reduce the size of the local variable to 8 or 16 bits after each assignment. This is called sign-extending for signed variables and zero extending for unsigned variables. It is implemented by shifting the register left by 24 or 16 bits, followed by a signed or unsigned shift right by the same amount, taking two instructions (zero-extension of an unsigned char takes one instruction).

These shifts can be avoided by using int and unsigned int for local variables. This is particularly important for calculations which first load data into local variables and then process the data inside the local variables. Even if data is input and output as 8- or 16-bit quantities, it is worth considering processing them as 32bit quantities

47.

How Are Local And Global Variables Are Allocated By Compiler.

Answer»

Normally, cpu registers are allocated for local variables, but if the address of that local variable is used by some code, then it won’t ALLOCATE register but access from memory, THUS result in un-optimized code. Gobal variables always use memory, so access is slower, so always use global only when it is absolutely NECESSARY.

Normally, cpu registers are allocated for local variables, but if the address of that local variable is used by some code, then it won’t allocate register but access from memory, thus result in un-optimized code. Gobal variables always use memory, so access is slower, so always use global only when it is absolutely necessary.

48.

What Is Loop Unrolling?

Answer»

Small loops can be unrolled for higher performance, with the disadvantage of increased codesize. When a LOOP is unrolled, a loop counter needs to be updated LESS often and fewer branches are executed. If the loop iterates only a few times, it can be fully unrolled, so that the loop overhead completely disappears.

int CountBitOne(uint n) { int bits = 0; while (n != 0) { if (n & 1) bits++; n &GT;> = 1; } return bits; } int CountBitOne(uint n) { int bits = 0; while (n != 0) { if (n & 1) bits++; if (n & 2) bits++; if (n & 4) bits++; if (n & 8) bits++; n >> = 4; } return bits; }

Small loops can be unrolled for higher performance, with the disadvantage of increased codesize. When a loop is unrolled, a loop counter needs to be updated less often and fewer branches are executed. If the loop iterates only a few times, it can be fully unrolled, so that the loop overhead completely disappears.

49.

Which Is The Best Way To Write Loops?

Answer»

The best way is to write count down loops and compiler can generate better machine code for it than the count up loops. In count down at loop TERMINATION, it NEEDS to generate one INSTRUCTION (SUBS), which SUBTRACTS as well as check the zero flag, but in count up case it has to add and compare with a constant, which takes TWO instructions.

The best way is to write count down loops and compiler can generate better machine code for it than the count up loops. In count down at loop termination, it needs to generate one instruction (SUBS), which subtracts as well as check the zero flag, but in count up case it has to add and compare with a constant, which takes two instructions.

50.

What Is A 'const' Variable?

Answer»

In simple terms 'const' MEANS 'read only'. Its value is not changed by any part of the code executed. So COMPILER can optimize by taking this info, and also it will GIVE WARNING when user TRY to change the value of this variable by mistake.

In simple terms 'const' means 'read only'. Its value is not changed by any part of the code executed. So compiler can optimize by taking this info, and also it will give warning when user try to change the value of this variable by mistake.

Previous Next