当前位置:网站首页>【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 .
边栏推荐
- mariadb spider分片引擎初体验
- zabbix怎样自定义mysql监控项并触发告警
- 合并K个升序链表---2022/02/26
- Service learning notes 03 front desk service practice
- Dynamic: capturing network dynamics using dynamic graph representation learning
- threejs利用indexeddb缓存加载glb模型
- How ZABBIX can customize MySQL monitoring items and trigger alarms
- Test and analysis of tidb write hotspot
- TestPattern error
- [collect first and use it sooner or later] 100 Flink high-frequency interview questions series (III)
猜你喜欢
随机推荐
【先收藏,早晚用得到】100个Flink高频面试题系列(三)
智能化整体图例,布线、安防、广播会议、电视、楼宇、消防、电气图的图例【转自微信公众号弱电课堂】
TestPattern error
tidb-数据误删恢复的几种方式
How ZABBIX can customize MySQL monitoring items and trigger alarms
Service learning notes 04 other service implementation methods and alternative methods
【先收藏,早晚用得到】100个Flink高频面试题系列(一)
The top ten trends of 2022 industrial Internet security was officially released
Tidb GC related problems
Mathematical foundations of information security Chapter 3 - finite fields (I)
Expérience initiale du moteur de segmentation de l'araignée mariadb
Mathematical basis of information security Chapter 1 - Division
vulhub
R language mice package error in terms Formula (TMP, simplify = true): the model formula in extractvars is incorrect
tidb-lightning配置数据还原路由
Threejs uses indexeddb cache to load GLB model
tidb-cdc日志tables are not eligible to replicate
Ffmpeg parity field frame interlace progressive command and code processing
There are so many open source projects. This time, I'll show you the differences between different versions and understand the meaning of alpha version, beta version and RC version
6-2 reverse output of multiple integers recursion









