c 位域
来源:互联网 发布:php网页源代码加密 编辑:程序博客网 时间:2024/06/16 06:36
#include<stdio.h>struct Date1{ unsigned int year;//4个字节 unsigned int month; unsigned int day; unsigned int hour; unsigned int minute; unsigned int second;};struct Date2{ unsigned int year ; unsigned int month: 4;//4bit unsigned int day: 5; unsigned int : 0;//空域,下面的从下一单元开始存放 unsigned int hour: 5; unsigned int minute: 6; unsigned int second: 6;};struct Date3{ unsigned int year ; unsigned int month: 4; unsigned int day: 5; unsigned int : 0;//空域,下面的从下一单元开始存放 unsigned int hour: 5; unsigned int minute: 6; unsigned int second: 30;//注意,此处不能超过一个int的位数大小(本机为32bit)};int main(){ printf("%d\n",sizeof(struct Date1)); printf("%d\n",sizeof(struct Date2)); printf("%d\n",sizeof(struct Date3)); return 0;}The result is :
241216Process returned 0 (0x0) execution time : 0.196 sPress any key to continue.
位域的讲解可以参考
http://hi.baidu.com/xiao1dian/item/7cfa8e0e9d0e51cc905718ed
http://tonybai.com/2013/05/21/talk-about-bitfield-in-c-again/
例2:
#include <stdio.h>//#pragma pack(push,1)//#pragma pack(push,2)#pragma pack(push,4)struct A{char t:4;char k:4;unsigned short i:8;unsigned long m;};#pragma pop()int main(){printf("%d",sizeof(A));return 0;}the result is
6、6、8
参考http://bbs.csdn.net/topics/330138880
0 0
- C位域
- C/C++位域
- C语言 位域
- c语言位域
- c/c++位域
- C/C++位域
- c语言位域
- C语言位域
- C 位域
- c位域
- c中的位域
- c位域
- C位域总结
- C语言 位域
- c位域
- C语言位域!
- c语言--位域
- C语言位域
- 面向对象语言【33】---工厂模式 【34】----构造函数
- oracle 常用的语句
- Verilog HDL无符号数和有符号数运算
- miniGUI3.0.12移植到Hi3531
- 深克隆与浅克隆
- c 位域
- 【面试】Ready GO
- 《软件工程核心知识》--全书格局
- 备战互联网2014校招 求职攻略大汇总
- pat 1069
- 面向对象编程基础 五 抽象函数和抽象类
- php中include和require区别
- 通过AJAX和PHP,提交JQuery Mobile表单
- jQuery .tmpl(), .template()学习资料小结