集整数值位掩码 [英] set integer value as bit mask

查看:104
本文介绍了集整数值位掩码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是指定位掩码为整数值简单的方​​法?
比如我要与第一,第三和第四位= 1的整数,其他= 0。

What is simplest way to assign a bit mask to integer value? For example I want integer with first, third and forth bits = 1, other = 0.

当然,我找了code,而不是单一的价值!肯定有很多可能性,但我试图找到最简单,最描述寻找

Certainly I am looking for code, not for single value! And certainly there lot of possibilities, but I try to find simplest and most descriptive looking

推荐答案

我觉得认为最好的办法是从0到刚刚指数位,然后应用以设置为N(!):个位,按位或用值(1 <<;&所述; n)的

I think the best way to think (!) is to just index bits from 0, and then apply "to set the n:th bit, bitwise-OR with the value (1 << n)":

int first_third_and_fourth = (1 << 0) | (1 << 2) | (1 << 3);

这篇关于集整数值位掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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