移位运算符
我遇到过这样一道编程面试题。但对我来说,你如何知道可以在这里使用位移位并不明显。 有好心人解释一下。谢谢。
数组的大小为 N,整数介于 0 到 1024 之间(允许重复)。另一个整数数组的大小为 M,对数字没有限制。查找第一个数组的哪些元素出现在第二个数组中。 (如果您使用额外的内存,请考虑使用按位运算符将其最小化)
我想知道在现实世界中位移运算符的含义是什么。以及如何识别需要位移方法的问题。
谢谢 桑杰
I came across such a programming interview question. But it is not obvious to me how you know bit shift can be used here.
Someone kindly explain. Thanks.
An array is of size N with integers between 0 and 1024(repetitions allowed). Another array of integers is of size M with no constraints on the numbers. Find which elements of first array are present in the second array. (If you are using extra memory, think of minimizing that still, using bitwise operators)
I would like to know what in the real world the bitshift operators mean. And how to identify the problems that require a bitshift approach.
Thanks
Sanjay
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这确实是一个非常简单的面试问题。因为您知道第一组中最多有 1025 个不同的整数,所以您可以使用该位数来表示每个数字是否在输入集中找到。因此,如果您希望答案将每个不同的数字恰好打印一次,逻辑是:将
现在,创建一个 1025 的位集您的语言可能不直接支持位,因此有时您必须使用字节数组,每个字节有 8 位。然后,假设您要设置位“k”,您将在索引 k / 8 处找到该字节,然后将该位设置在位置 k % 8 处。要执行后者,您必须从位位置(0 到7)到一个位值(位0代表值1,位1代表值2,位2代表值4...位7代表值128 - 2的所有幂)。要获得这些值,您需要将数字 1 向左移动“位位置”。所以, 1 << 0 仍然是 1,而 1 << 7 是 128。然后,您可以:
if (array [k / 8] & (1 << (k % 8))) ...it's on...
array[k / 8] |= (1 << (k % 8));
如果数组中的值恰好少于 1025 个,则有更有效的方法可以做到这一点要么设置,但这是一个很好的通用解决方案。
It's a very easy interview question really. Because you know there's at most 1025 distinct integers in the first set, you can use that number of bits to represent whether each of those numbers is found or not in input sets. So, if you want the answer to print each distinct number exactly once, the logic is:
Now, to create a bitset of 1025 bits might not be supported directly by your language, so what you sometimes have to do is use an array of bytes, each of which has 8 bits. Then, say you want to set bit "k", you'll find the byte at index k / 8, then set the bit at position k % 8. To do the latter, you have to convert from a bit position (0 through 7) to a bit value (bit 0 represents value 1, bit 1 value 2, bit 2 value 4... bit 7 value 128 - all the powers of two). To get these values, you take the number 1 and bitshift it left by the "bit position". So, 1 << 0 is still 1, while 1 << 7 is 128. You can then:
if (array[k / 8] & (1 << (k % 8))) ...it's on...
array[k / 8] |= (1 << (k % 8));
There are more efficient ways to do this if there happens to be a lot less than 1025 values in either set, but this is a good general solution.
位移位运算符的工作原理如下。想象一下你有一个整数值X。这个X将以二进制形式表示,即1和0。话后按班次算。职位数量将被移动。
访问此链接 http://www.php.net/manual/en/ language.operators.bitwise.php 他们有一些关于这种移位运算符如何工作的示例。
Bitshift operators work like this. Imagine you have an integer value X. This X will be represented in Binary form , which is 1 and 0's. After words according to the shift operator. Number of positions will be moved.
Visit this link http://www.php.net/manual/en/language.operators.bitwise.php they have some examples on how this shift operators work.