在在C字符访问位 [英] Access bits in a char in C

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

问题描述

我有一个十六进制数0x37符号和二进制再presentation是0011 0111我如何访问二进制重新presentation这是11的第2位?我如何使用位移位或屏蔽来实现这一目标?我可以通过位访问位,但在人们不两位去了?

I have a hex number 0x37 and its binary representation is 0011 0111. How do I access the first 2 bits of the binary representation which is "11"? How do I use bit shifting or masking to achieve this? I can access bit by bit but not two bits at one go?

推荐答案

如果您&放大器; 与×03你的号码,你会得到最后两位。

If you & your number with 0x03, you will get the last two bits.

char c = 0x37;
char mask = 0x03;
char lastTwo = c & mask;

这篇关于在在C字符访问位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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