interview – Bread & Cup https://blog.breadncup.com Sat, 26 Nov 2011 16:35:18 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.2 148801731 Make your own atoi() function in C https://blog.breadncup.com/2009/12/21/make-your-own-atoi-function-in-c/ https://blog.breadncup.com/2009/12/21/make-your-own-atoi-function-in-c/#comments Tue, 22 Dec 2009 03:51:03 +0000 http://blog.breadncup.com/?p=225 atoi() is able to translate a string of a number into an integer number. There are lots of tech to do, and here is showing some examples.

int my_atoi(char *p) {
  int k = 0;
  while (*p) {
    k = k*10 + (*p) - '0';
    p++;
  }
  return k;
}

10 times can be switched to bit-wise shift since bit-wise will use smaller instruction set which enables to reduce transistor in a system. Following shows of it.

int my_atoi(char *p) {
 int k = 0;
 while (*p) {
 k = (k<<3)+(k<<1)+(*p)-'0';
 p++;
 }
 return k;
}
]]>
https://blog.breadncup.com/2009/12/21/make-your-own-atoi-function-in-c/feed/ 17 225