最有效的方法来提取位标志 [英] Most efficient way to extract bit flags

查看:130
本文介绍了最有效的方法来提取位标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些可能的位标志。

  1,2,4,8,16,64,128,256,512,2048,4096,16384,32768,65536

所以,每一个数字就像是在服务器端真/假声明。因此,如果前3项,并且只有前3项被标记为真,在服务器端,web服务将返回7.或者,如果上述所有14个项目是真实的,但我仍然会得到一个单一的数字从背面网络服务,这是是所有这些数字的总和。

什么是处理我回去找出哪些项目被标记为数字的最好方式真实?


解决方案

 如果(7和1){//如果第1位被设置在返回的数字(7)}

I have these possible bit flags.

1, 2, 4, 8, 16, 64, 128, 256, 512, 2048, 4096, 16384, 32768, 65536

So each number is like a true/false statement on the server side. So if the first 3 items, and only the first 3 items are marked "true" on the server side, the web service will return a 7. Or if all 14 items above are true, I would still get a single number back from the web service which is is the sum of all those numbers.

What is the best way to handle the number I get back to find out which items are marked as "true"?

解决方案

if (7 & 1) { // if bit 1 is set in returned number (7)

}

这篇关于最有效的方法来提取位标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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