将位转化为值

发布于 2024-11-15 21:12:00 字数 404 浏览 7 评论 0原文

计算机如何知道 (int x, y) x << y 表示移位 y 位?我指的不是转移部分。我的意思是 y 部分。计算机是否会将 x 移动 1 并从 y 中减去 1,直到 y == 0?如果不是,计算机如何计算出 y 的值?

如果说y = 10,则二进制表示为0b1010。 计算机不能简单地获取1010的每一位并使用它,不是吗?

我正在尝试对大于 8 的位大小执行此操作。由于这些值不是简单地存储为标准整数数组,因此容器不表示值,因此重载运算符 <<>> 有点难。 然而,从 100 位数字倒数到 0 效率有些低,所以我试图找到一种方法让计算机更快地理解位数组。

How does a computer know that (int x, y) x << y means shift over y bits? I don't mean the shift part. I mean the y part. Does the computer shift x by one and subtract one from y until y == 0? If not how does the computer figures out the value of y?

If say y = 10, then the binary representation is 0b1010.
The computer can't simply take each bit of 1010 and use it, can it?

I am trying to to this for bit sizes larger than 8. Since the values are not simply stored as arrays of standard integers, the container doesn't represent a value, so overloading the operators << and >> is a bit harder.
However, counting down from a 100 bit number to 0 is somewhat inefficient, so I'm trying to find a way to make the computer understand a bit array faster.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭白衣梦中忆 2024-11-22 21:12:00

首先,在 C 中,执行大于相关类型位宽的移位的效果是未定义的 - 换句话说,如果您有 32 位整数,x << 33 将导致不可靠的结果(它不必为零!)。

确切的实现取决于您的硬件。一些嵌入式处理器确实执行单位移位循环;然而,在更强大的 CPU 架构(例如 x86)上,有一个机器指令可以在单个操作中执行任意移位,通常使用类似 硬件中的桶形移位器。 C 对移位操作数的值的限制来自于不同的指令集对超出范围的移位值的处理方式不同; x86 将截断移位参数(即,如果您使用 32 位值,则执行模 32),但其他一些指令集架构可能有不同的行为。

一般来说,除非您正在开发嵌入式处理器,否则您无需担心单个位移位的成本会很高。

First off, in C the effects of performing a shift larger than the bit width of the type in question are undefined - in other words, if you have 32 bit ints, x << 33 will result in unreliable results (it need not be zero!).

The exact implementation depends on your hardware. Some embedded processors do indeed perform a loop of single bit shifts; on more powerful CPU architectures such as x86, however, there is a machine instruction to do an arbitrary shift in a single operation, usually using something like a barrel shifter in hardware. The C limitation on the value of the shift oprand comes from different instruction sets handling out-of-range shift values differently; x86 will truncate the shift argument (ie, doing modulo 32 if you're using a 32-bit value), but some other instruction set architectures may have different behavior.

In general, unless you're developing for an embedded processor, you don't need to worry about a single bit shift being expensive.

独自←快乐 2024-11-22 21:12:00

你是说你有 2 位数组并且你试图为它们创建一个移位运算符?最简单的方法可能是将正确的 y 转换为整数,然后将 x 中的每一位移动该数量......否则如果你想一次对它进行一位操作,效率会更低。你会看第一个位,如果它是 1,则移位一次,看第二位,如果它是 1,则移位两次...我认为你必须将其作为一个整体来处理它是高效的。


假设 y 无法容纳整数...这只是我的想法,但让我们假设 int 只有 2 位,但是 y0b1111。然后,取出前 2 位,将其转换为整数 (3) 并将 x 移位该量。然后将 y 右移 2(int 的大小),并将其转换为 int。这又变成了 3,你必须重复 4 次 (int max + 1),总共 12 次。+我们之前做的 3 意味着我们已经移动了 x 15 次是 y 的值。您可以对更大的数字重复此过程。

you're saying you have 2 bit arrays and you're trying to create a shift operator for them? the easiest way to do it would probably be to convert the right one, y, into an integer and then shift every bit in x by that amount.....otherwise if you want to operate on it 1 bit at at time it'll be even less efficient. you'll look at the first bit, if its 1, shift once, look at the second bit, if its 1, shift twice...i think you have to deal with it as a whole for it to be efficient.


assuming y can't fit in an integer...and this is just off the top of my head, but let's assume an int is only 2 bits, but y is 0b1111. then, take the first 2 bits, convert it to an integer (3) and shift x by that amount. then right-shift y by 2 (the size of our int), and convert that to an int. this makes 3 again, which you'd have to repeat 4 times (int max + 1) for a total of 12. that, + the 3 we did earlier means we've shifted x 15 times which was the value of y. you can repeat this process for even larger numbers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文