C:建立一个字节 [英] C: Building a byte

查看:115
本文介绍了C:建立一个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有16个元素的数组。我想这些评价为布尔0或1,然后存储在这2个字节,所以我可以写一个二进制文件。我该怎么做呢?

I have an array with 16 elements. I would like to evaluate these to a boolean 0 or 1 and then store this in 2 bytes so i can write to a binary file. How do I do this?

推荐答案

像这样的事情,你是什么意思?

Something like this you mean?

unsigned short binary = 0, i;
for ( i = 0; i < 16; ++i )
  if ( array[i] )
    binary |= 1 << i; 

// the i-th bit of binary is 1 if array[i] is true and 0 otherwise.

这篇关于C:建立一个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆