当前位置:网站首页>【C】 ATOI function implementation +offsetof implementation + exchange binary odd and even digits

【C】 ATOI function implementation +offsetof implementation + exchange binary odd and even digits

2022-06-11 18:00:00 Penguins don't cry

Catalog

Preface

One 、atoi Function implementation

Two 、 Using macros to exchange odd and even bits of an integer

  3、 ... and 、offsetof Realization

summary


Preface


One 、atoi Function implementation

         atoi() Function functions : Convert a string to an integer ;atoi() Will scan parameters nptr character string , Skip the preceding space character , The conversion does not begin until you encounter a number or a sign , And when you encounter a non number or string ('\0') To end the transformation , And return the result ( Returns the converted integer number ).

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//VALID  Indicates that the result is legal 
//INVALID  Indicates that the result is illegal 
enum State
{
 VALID,// legal 
 INVALID// illegal 
};
// The default result may be illegal , Change to legal after correct conversion 
enum State state = INVALID;
​
int my_atoi(char *str)
{
 // Represents the positive and negative of a number 
 int flag = 1;
// Long integer guarantees no overflow 
 long long ret = 0;
 assert(str);
 state = INVALID;
  // Skip white space 
 while(isspace(*str))
 {
  str++;
 }
 if(*str == '\0')
 {
  return 0;
 }
  // Skip sign 
 if(*str == '+')
 {
  str++;
 }
 else if(*str == '-')
 {
  flag = -1;
  str++;
 }
  // Start converting numeric characters until non numeric characters 
 while(isdigit(*str))
 {
  ret = ret * 10 + flag * (*str-'0');
  if((ret > INT_MAX) || (ret < INT_MIN))
  {
   return 0;
  }
  str++;
 }
  // Normal stop 
 if(*str == '\0')
 {
  state = VALID;
  return (int)ret;
 }
 else
 {
    // Non numeric characters encountered 
  return (int)ret;
 }
}
​
int main()
{
 char *p = "-1212212121212";
 printf("%d\n", my_atoi(p));
 return 0;
}

Two 、 Using macros to exchange odd and even bits of an integer

Ideas :

        / /00000000000000000000000000001010 - 10

        // hold 10 All even bits of are reserved , Odd position 0, Move one more bit to the right 1

        therefore & On 0xaaaaaaaa(10101010101010101010101010101010)

        //00000000000000000000000000001010 >>1

        //00000000000000000000000000000101

        // hold 10 All the odd digits of are preserved , Even position 0, Move left one more bit

        // therefore & On 0x55555555(010101010101010101010101010101)

        //00000000000000000000000000000000 <<1

        //00000000000000000000000000000000

#define SwapIntBit(n) (((n) & 0x55555555) << 1 | ((n) & 0xaaaaaaaa) >> 1)

※ This macro can only complete 32 Shaping within bits , To finish 64 Bit , Then we will 5 and a Just double the number of  

  3、 ... and 、offsetof Realization

This macro is used to find the offset of a member in the structure

size_t offsetof( structName, memberName );
// The first parameter is the name of the structure , The second parameter is the name of the structure member . The macro returns the structure structName Member of the memberName The offset . The offset is size_t Type of .

  Realization :

#define offsetof(s, m) (size_t)&(((s *)0)->m)

StructType Is the structure type name ,MemberName Is the member name . The specific operation method is :

1、 First the 0 Pointer converted to a structure type , Equivalent to the first address of a structure is 0. here , The offset of each member becomes relative 0 The offset , So you don't need to subtract the first address .

2、 Use... For this pointer -> Visit its members , And take out the address , Since the starting address of the structure is 0, At this point, the member offset is directly equivalent to 0 The offset , So the value obtained is directly the offset of the first address .

3、 Take out the address of the member , Strong conversion size_t And print , So we get this offset .


summary

        It is suggested to review again .

原网站

版权声明
本文为[Penguins don't cry]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011854587520.html