转换了一套布尔的多项 [英] Converting a set of booleans to a number

查看:89
本文介绍了转换了一套布尔的多项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code我将尽全力采取一组三个布尔值,并将其转换成switch语句一个int:

This is the code I am going to use to take a set of three booleans and convert it into an int for a switch statement:

int bits = 0;
bool a = true, b = false, c = true;  // 101 = 5

bits = bits | a << 2;
bits = bits | b << 1;
bits = bits | c;

cout << bits;

我有一个基于这三个布尔值的组合状态8案件。我这样做对吗?

I have eight cases based on the combined state of these three booleans. Am I doing this right?

右键,而不是在语法意义上的,虽然如果有任何问题,也请告知。在意义上更正确的这是解决这个问题的最好方法?

Right, not in the sense of the syntax although if there are any problems there please advise. More right in the sense of "Is this the best way to solve this problem?"

推荐答案

如果您使用的是C ++,你可以使用位集&LT; N方式&gt;

If you are using C++, you could use bitset<N>.

这篇关于转换了一套布尔的多项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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