__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;
}

 

發佈了598 篇原創文章 · 獲贊 285 · 訪問量 50萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章