Technical Interview Questions and Answers :: C
21 / 123
For loop is used when we know how many times
22 / 123
to check if the ith bit is on or off in a number n following bitwise operation is used.
flag = n&(1<<i) ;
if the value of flag is zero then the bit is set otherwise not.
explanation of above approach is as follows.
1. lets assume n is 9 so its bitwise operator is 1001 and bitwise representation of 1 is 0001
2.to check if 4th bit is on we do left shift by 4 on 1 (1<<4) which shift the bits in one to left by 4 unit now the (1<<4)=1000
3.Finally bitwise and operation is performed so 1001&1000 = 1000 which is not zero so it means that 4th bit of 9 is set
23 / 123
The bitwise operator xor i.e '^' is used to turn off a particular number
by performing the following operations say we want to turn the ith bit off in a number n
then n = n^(1<<i)
24 / 123
multiplying a number by two is equivalent to bitwise left shift of a number by 1 for eg
1 has a binary representation as 00001 left shift by 1 will make it 00010 which is 2.
right shift is equivalent to divide a number by 2 .
left shift is equivalent to multiply a number by 2.
25 / 123
Enumarations are list of integer constants, by default value is zero.
26 / 123
Structure is a function which contains different types of data types and allocation different memory location.
27 / 123
Structure- It is an user defined data function that allow to store multiple type of data in a single unit.
It occupies sum of memory of all location.
Whereas, Union- It is also an user defined data function that allow to store multiple type of data in a single unit.
But it occupies memory of largest member only.
28 / 123
1.memory.
2.easy to declare.
29 / 123
Struct stack
{
30 / 123
#include<stdio.h>
struct name{
int a;
int b;
int c;
};
struct name func(){
struct name s1 ;
s1.a=1;
s1.b=2;
s1.c=3;
return s1;
}
int main(){
struct name val = func();
}