当前位置:网站首页>【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
One 、atoi Function implementation
Two 、 Using macros to exchange odd and even bits of an integer
3、 ... and 、offsetof Realization
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 .
边栏推荐
- 6-2 写文章(*)
- Rtsp/onvif protocol easynvr video platform arm version cross compilation process and common error handling
- 7-2 h0107. Pig-Latin
- Hash表、 继承
- [foundation of deep learning] learning of neural network (3)
- TiDB-unsafe recover(tikv宕机数大于等于一半副本数)
- After class, I looked at the document and went back to the lab. I picked up the forgotten SQL operators again
- Hello go (XII). Go language common standard library II
- The tle6389 step-down DC-DC switch controller has high efficiency in the whole load range of 1mA to 2.5A - keshijin mall
- Service learning notes 02- actual combat startservice and bindservice
猜你喜欢

送给大模型的「高考」卷:442人联名论文给大模型提出204个任务,谷歌领衔

Bracket generation ---2022/02/25

Leetcode force deduction question

Tle6389-2g V50's unique pwm/pfm control scheme has a duty cycle of up to 100%, forming a very low differential pressure - keshijin mall

Summary of clustering methods

Why is the UDP stream set to 1316 bytes

Automated testing selenium

【先收藏,早晚用得到】49个Flink高频面试题系列(二)

Install MariaDB 10.5.7 (tar package installation)

Chorus翻译
随机推荐
Kubernetes deploys elk and collects container logs using filebeat
Tle6288r is a 6-channel (150 MOhm) intelligent multi-channel switch using intelligent power technology - keshijin mall
Tidb CDC create task error unknown or incorrect time zone
TestPattern error
Hello go (XII). Go language common standard library II
The tle6389 step-down DC-DC switch controller has high efficiency in the whole load range of 1mA to 2.5A - keshijin mall
Several ways to recover tidb data from accidental deletion
[online problem] timeout waiting for connection from pool
【先收藏,早晚用得到】49个Flink高频面试题系列(二)
ArrayList collection, object array
ForEach遍历集合、 集合容器
6-3 batch sum (*)
开源项目那么多,这次带你了解个版本的区别,明白alpha版、beta版、rc版是什么意思
Comparison of mongoose in express, KOA and egg
Mathematical foundations of information security Chapter 3 - finite fields (I)
jsfinder,wafw00f安装,nmap配置(缺少msvcr120.dll文件)
Mathematical basis of information security Chapter 4 -- quadratic residual and square root
Service learning notes 01 start method and life cycle
合并两个有序链表---2022/02/24
密评-----