gpt4 book ai didi

c# - 在 C# 中,什么类似于 C++ 中的 CHAR_BIT?快速腹肌

转载 作者:太空宇宙 更新时间:2023-11-03 10:35:23 25 4
gpt4 key购买 nike

C# 是否有类似于 C++ 的 CHAR_BIT 的东西? ?

更新:
基本上,我试图在没有分支的情况下计算 abs,这是 C++ 版本:

// Compute the integer absolute value (abs) without branching

int v; // we want to find the absolute value of v
unsigned int r; // the result goes here
int const mask = v >> sizeof(int) * CHAR_BIT - 1;

r = (v ^ mask) - mask;

这是我的 C# 版本:

private int Abs(int value)
{
int mask = value >> sizeof(int) * 8 - 1;
return ((value ^ mask) - mask);
}

奇怪的是,这也有效:

private int Abs(int value)
{
int mask = value >> sizeof(int) * sizeof(byte) - 1;
return ((value ^ mask) - mask);
}

最佳答案

如果您认为 C# 中的 byte 等价于 C++ 的 char,那么最接近 CHAR_BIT 的等价物是 8。在 C# 中,一个 byte 保证正好是 8 位。

关于c# - 在 C# 中,什么类似于 C++ 中的 CHAR_BIT?快速腹肌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4883515/

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