C Secrets
C Secrets
The Solution:
short int x;
printf(“%d”,SIZE_OF(x));
Ans : 2
The Explanation :
Lets look how the answer is coming without sizeof(). First declare a short
int x variable. By using &x we get the base address of the variable x and by adding
1 to it we get the base address of next short int type. Hence the resulting address of
(&x + 1) will be 2 bytes more than the base address of the variable x. But if we just
display the difference between the base address of x and the incremented address,
then the difference will be 1 means“1 block of short int type has been added” but
we need the result of size of variable in terms of bytes not in terms of blocks. This
can be achieved if we typecast the address into char *, because address of char
datatype will always be in blocks of 1 byte, hence if the difference between the
base address of x and the incremented address is displayed in terms of char type ,
then the difference will be displayed as 2, because the difference is actually 2 blocks
or 2 bytes in terms of char type representation.
2 Cecrets
02 How can you measure the size of a type without using sizeof
operator?
The Solution:
Ans : 4
The explanation:
Whenever we typecast any constant, it converts the constant into a base
address of specified data type and as we know address + 1 will always return the
next address of its type or we can say the address of next block of memory of that
type, so accordingly if 1 will be added with the address 0 (as specified in (long int
*)0) then it will return the size of any data type in bytes. For example 1 for char, 2
for short integer, 4 for long integer or float and so on.
The Solution:
struct xx
{
char x:5;
};
void main()
{
struct xx a;
Cecrets 3
int counter=0;
clrscr();
a.x=1;
while(a.x) // till 0
{
a.x<<=1;
counter++;
}
printf(“%d”,counter);
getch();
}
Ans : 5
The explanation:
The sizeof operator can’t measure the size of a bit field.
So to get the output, here we have assigned 1 to the variable x which can
assign 1 only in 5 bits out of 8 bits. When the value of a.x is shifted towards left by 1,
the corresponding memory representation is depicted in Figure 2, where 1 has shifted
to the left by one place and 0 gets padded to the LSB. Figure 3 shows the memory
representation of a.x after getting shifted 4 times and if you notice the loop, the value
of counter is 4 as it gets incremented by 1 in each shift. And finally the value of a.x
becomes 0 in 5th shift, which makes the loop to terminate. At this stage the value in
the counter variable is 5 which is nothing but the size of the bit field.
The Solution:
void main()
{
int i=0,j=0;
4 Cecrets
float a;
clrscr();
((unsigned char*)&a)[j]=1;
while(a)
{
i++;
((unsigned char*)&a)[j]<<=1;
if(i%8==0)
{
j++;
((unsigned char*)&a)[j]=1;
}
if(a==0 && ((unsigned char*)&a)[j]==128)
i++;
}
printf(“%d”,i);
getch();
}
Ans : 32
The Explanation:
Size of a variable cannot be calculated using sizeof operator because it
returns the value in terms of bytes. Still then we can calculate the size of the variable
in terms of bits by checking each individual bit and increasing the counter. This is
possible by the value 1. As a bit can store only two values which is either 0 or 1. So
we check the value of the variable repeatedly for each bit with a value 1 and the
rest of the bit to zero. The moment the value of the variable becomes 0 the current
value in the variable counter contains the number of bits.
Cecrets 5
5 How many different algorithms that we can use to swap between
two variables?
Lets see….
The Solution:
Logic 1:
int a=5,b=10;
int c; //using extra memory space
c=a;
a=b;
b=c;
The above example shows the swapping of two variables a and b by using
extra memory space for variable c. In the first step the value of a is assigned to c,
thus the value of c now becomes 5, after that b is assigned to a and the value of a
now becomes 10 and finally value of c is assigned to b. Thus the values of a and b
have been swapped.
Logic 2:
a=a+b; // Using arithmetic operators
b=a-b;
a=a-b;
Logic 4:
asm mov ax,a;
asm mov bx,b;
asm mov cx,bx;
asm mov bx,ax;
asm mov ax,cx;
asm mov a,ax;
asm mov b,bx;
Logic 5:
a^=b^=a^=b; // Using one line statement
The solution:
x=x<<8 | x>>8;
The explanation:
Let value of x be 300.
x = 00000001 00101100
Fig.1
Fig.2
Fig.3
Fig.4
The Solution:
For example:
The Solution:
void main()
{
int number, ithbit, jthbit;
clrscr();
printf(“\nEnter a no. : “ );
scanf(“%d”,&number);
printf(“\nEnter the two positions : “ );
scanf(“%d%d”,&ithbit,&jthbit);
ithbit--;
jthbit--;
Cecrets 9
if( ((number & (1<<ithbit)) >>ithbit) !=
((number&(1<<jthbit))>>jthbit) )
{
number = number ^ (1<<jthbit);
number = number ^ (1<< ithbit);
}
printf(“The resultant no is %d”,number);
getch();
}
The Explanation:
let’s have a look at the program. Immediately after getting the bit number
from the user we have decremented each bit by one. It’s only because the actual bit
number starts from 0 where as for the user it can be considered as the 1st bit. When
the user will give to interchange the ith and jth bit, we have changed it to (i-1)th
and (j-1)th bit.
(number & (1 << ithbit)) >> ithbit gives the content of ithbit.
Let’s take an example as the user wants to interchange the content of 3rd bit
and 6th bit of 33.
Now the new value is 0000 0000 0000 0101 -> 3rd bit = 1 and
5th bit = 0 which were interchanged and the new value of number is 5.
In reality,the bit number actually starts from 0th bit but for the user it can be
considered to start from the 1st bit.
The Solution:
Logic 1:
#include<limits.h>
int convert(long int n,long int m)
{
if(m==1<<31)
{
return;
}
(n & m)?convert(n,m*2),printf(“1
“):(convert(n,m*2),printf(“0 “));
return 1;
}
Case 2:
void main()
{
unsigned long int x;
int i;
printf(“\nEnter a long integer : “);
scanf(“%ld”,&x);
for(i=0;i<32;i++)
printf(“%d”,x<<i>>31);
getch();
}
The Solution:
void main( )
{
int x ;
printf(“\nEnter any number : “);
scanf(“%d”,&x);
printf(“%d”,x>>2);
}
Ans :
Enter any number : 16
4
The explanation:
We know that if we right shift a number by 1 we get the result which is
same as the number divided by 2. So to get the result of dividing a number by 4 we
have to left-shift the number by 2. This is a more efficient or faster way of getting
the result because it directly interacts with the bits of the number.