gpt4 book ai didi

c# - BitArray 改变范围内的位

转载 作者:行者123 更新时间:2023-11-30 17:48:19 25 4
gpt4 key购买 nike

如何确保从 BitArray 更改位时,BitArray 值保持在一个范围内。

示例:

Given the range [-5.12, 5.12] and

a = 0100000000000000011000100100110111010010111100011010100111111100 ( = 2.048)

通过在随机位置改变一点,我需要确保新值保持在给定范围内。

最佳答案

我不是 100% 确定您在做什么,并且此答案假定您当前将 a 存储为 64 位值 (long)。以下代码可能会帮助您指明正确的方向。

const double minValue = -5.12;
const double maxValue = 5.12;

var initialValue = Convert.ToInt64("100000000000000011000100100110111010010111100011010100111111100", 2);
var changedValue = ChangeRandomBit(initialValue); // However you're doing this

var changedValueAsDouble = BitConverter.Int64BitsToDouble(initialValue);
if ((changedValueAsDouble < minValue) || (changedValueAsDouble > maxValue))
{
// Do something
}

关于c# - BitArray 改变范围内的位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23249240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com