Chinaunix

标题: 位域疑问? [打印本页]

作者: _nosay    时间: 2017-04-11 22:57
标题: 位域疑问?
  1. #include <stdio.h>

  2. struct tcpflag
  3. {
  4.         char syn:1,
  5.              ack:1,
  6.              psh:1,
  7.              urg:1,
  8.              rst:1,
  9.              fin:1,
  10.              idx:1,     /* verified?? */
  11.              dir:1;     /* verified?? */
  12. };

  13. struct test {
  14.         struct tcpflag flag;
  15. };

  16. int main()
  17. {
  18.         struct test t = {{ .syn=1, .ack=1, .rst=1, }};

  19.         printf("%d,%d,%d,%d,%d,%d,%d,%d\n",
  20.                 t.flag.syn, t.flag.ack, t.flag.psh, t.flag.urg, t.flag.rst, t.flag.fin, t.flag.idx, t.flag.dir);

  21.         return 0;
  22. }
复制代码


运行结果:
[root@localhost 9]# ./a.out
-1,-1,0,0,-1,0,0,0


为什么是-1?

作者: dreamtale90    时间: 2017-04-12 07:23
char,unsigned char的符号位问题?

作者: _nosay    时间: 2017-04-12 10:58
回复 2# dreamtale90

腻害
作者: nswcfd    时间: 2017-04-13 10:34
unsigned,踩过这个坑
作者: _nosay    时间: 2017-04-13 23:06
回复 4# nswcfd






欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2