Skip to main content

Posts

Showing posts from 2012

Some interesting questions

1.whats the value of x. x = 20; y=30; x=y++ + x++; x = 30++ + 20 ++ = 50 but there is one ++ pending for x which shall be executed so the value of x will 51 . 2.write  a function to check its a big endian or little endian first method union test{         int i;         char c; }; test t1; t1.i = 1; if(t1.c == 1){     pf("LE"); } else pf("BE"); second method. int i=1; if( *(char*)&i == 1) pf("LE"); else pf("BE") 3.write a program to reverse the bits in an integer 1101 ==> 1011  int output; for(int i=0;i<32;i++){        if( input & 1 ==1 ){              output |= 1<<32-i;        }      input = input >> 1; } 4.  int inc(int ip){      static int i=2;      i += ip;      return i; } x = 10; if(  inc(x) == 12 || inc(x) == 14 ) pf("%d",inc(10)) ; whats is the output?  its quite easy .. let me know if u need answer. 5.Think about the reason for one thread not rec

How to find if a number is power of 2?

Mxl Mxl-Fr-402 Table Interview Boundary Microphone For Field Recording (Google Affiliate Ad) Your writing a normal client server program and there is  a problem, the data length you are expecting is a power of 2. So now how can we check if it is  a power of 2 ? how how how? i thought thought and thunk :P and there were no answers, so at that time what one should do? ask everyone you know and find best answer and this one is the best i guess !!. So here is the solution : Let n be the number which you want to test .... wait wait wait .... just now i asked my roomie abt this problem and he gave me one more solution that too on the fly. I think it should work, we just checked it with numbers up to 1024 so it seems the logic is good !! but i don't know how efficient it will be if one wants to implement it. Solution given by my roomie goes like this. Convert the given number to binary and if it has just one bit as 1 tada !! its  a power of 2 !! so i think one can implement it this

atoi implementation in C

Algo for you own implementation of atoi :) :) int val = 0;    for each char in the string       val = val *10 + char-48 lets write some code for it :) int myatoi(char *convetIt) {      int val = 0;      while(*convetIt)      {           val = (val * 10) + *convetIt - 48;           convetIt++;      } } So how it works? lets see how it works take an example of string "123" formula : val = (val * 10 ) + *convetIt - 48                 val = (0 * 10 ) + '1' - 48      =>      0*10  + 49 - 48 =  0  + 1 = 1                 val = (1 * 10)  + '2' - 48      =>     1*10  + 50 - 48 = 10  + 2 = 12                 val = (12 * 10 ) + '3' - 48    =>   12*10 + 51 - 48 = 120 + 3 = 123 so val contains 123 now :) . so how come '1' '2' '3' is replaced with 49,50,51 ? they are the asci values of these characters ,hence this logic. There is some efficient way to implement this , lets see if anyone comes up with th