【发布时间】:2015-01-10 03:51:22
【问题描述】:
布尔数组为 1 为真,为 0 为假。 8 将表示为 false false false true,其中 true 位于索引 3。6 将表示 false、true、true。我也想在不使用 pow() 的情况下做到这一点。该方法将以整数形式返回十进制表示。
到目前为止我所拥有的:
int Binary::binaryToInteger(bool *binaryArray, int size)
{
Something that will keep track of the index and something that will keep track of
the amount I need to multiply by added to a total and an if else that will take care
of true or false
return total;
}
感谢您的帮助!
【问题讨论】:
-
输入数组中的数字是否有符号表示?
-
@MarkB:
int的符号在其最高位。因此,如果数组大小为<= numeric_limits<int>::digits,则该数组表示一个无符号数。如果数组的大小是== numeric_limits<int>::digits+1,则最后一个数组元素指定符号。numeric_limits<int>::digits不计算符号位。