__builtin_popcount(s)

#include <stdio.h>

//计算一个 32 位无符号整数有多少个位为1
#define popcount(s) __builtin_popcount(s)

typedef struct {
	int pid;
	int sockfd;
}mac_t;

typedef struct {
	int id;
	int fd;
	int pid;
	mac_t mac;
}cell_t;

int main()
{
	cell_t cell = {
		.id = 1234,
		.fd = 21,
		.mac.pid = 121212,
	};

	printf("%d\n", popcount(&cell));

	return 0;
}
发布了591 篇原创文章 · 获赞 285 · 访问量 49万+

猜你喜欢

转载自blog.csdn.net/Rong_Toa/article/details/104270402