简体   繁体   English

Javascript在32位浏览器中计算两个64位数字之间的逻辑与?

[英]Javascript calculate logical AND between two 64bit numbers in 32bit browser?

I am running Sharepoint 2007 farm and am trying to calculate user permissions. 我正在运行Sharepoint 2007服务器场,并正在尝试计算用户权限。 I have read This post about the particular metadata field where I am getting my mask from. 我看了帖子在哪里我从获得我的面具特定的元数据字段。 And I have been looking at The following guide in order to see what masks I need to compare to. 我一直在看以下指南,以了解我需要与哪些口罩进行比较。

Here is my delima, whenever I run the following code in a IE javascript console I get back 0: 这是我的想法,每当我在IE JavaScript控制台中运行以下代码时,我都会得到0:

((0x4000000000000000).toString(16) & (0x400001F07FFF1BFF).toString(16))

Now I know this is incorrect because the respective binary values are: 现在我知道这是不正确的,因为相应的二进制值为:

100000000000000000000000000000000000000000000000000000000000000
100000000000000000000011111000001111111111111110001101111111111

Which should equal 哪个应该相等

100000000000000000000000000000000000000000000000000000000000000

I have also put this into my windows calculator just to make sure I wasn't crazy (and to get those super long binary numbers). 我也将其放入Windows计算器中只是为了确保我没有发疯(并获得那些超长二进制数)。

NOTE As I got to this line I realized that my browser is 32bit (which is a requirement for the site I am using this on) and this is a 64 bit number! 注意当我到达这一行时,我意识到我的浏览器是32位的(这是我在其上使用该网站的要求),并且这是64位的数字!

How can I (preferably in one line) calculate the bitwise AND of a two 64bit numbers using a 32bit browser? 我如何(最好在一行中)使用32位浏览器计算两个64位数字的按位与?

I do know that I could convert the number into a binary String and utilize a loop to check each bit but is there a simpler method? 我确实知道我可以将数字转换为二进制String并利用循环检查每个位,但是有没有更简单的方法?

EDIT - Solution Utilizing the information from This Post and the answer below I came up with the following solution: 编辑-解决方案利用从信息此文章和我下面的回答提出了以下解决方案:

     var canEdit = false;
var canEditMask = [0x00000000,0x00000004];
var canApprove = false;
var canApproveMask = [0x00000000,0x00000010];
var canRead = false;
var canReadMask = [0x00000000,0x00000001];
var canDesign = false;
var canDesignMask = [0x00000000,0x00000800];
var mask = [originalmask.substring(0,10).toString(16),
                  ("0x"+itemperms.substring(9)).toString(16)];
canEdit = (mask[0] & canEditMask[0]) >0 || (mask[1] & canEditMask[1]) >0;
canRead = (mask[0] & canReadMask[0]) >0 || (mask[1] & canReadMask[1]) >0;
canDesign = (mask[0] & canDesignMask[0]) >0 || (mask[1] & canDesignMask[1]) >0;
canApprove = (mask[0] & canApproveMask[0]) >0 || (mask[1] & canApproveMask[1]) >0;

I hit the same problem this evening, so I wrote the following to allow bitwise AND and OR of values above 2^32: 我今天晚上遇到了同样的问题,所以我写了以下内容以允许2 ^ 32以上的值按位与和或:

    function bitand(val, bit) {
        if (bit > 0xFFFFFFF || val > 0xFFFFFFF) {
            var low = val & 0xFFFFFFF;
            var lowbit = bit & 0xFFFFFFF;
            val /= 0x10000000;
            bit /= 0x10000000;
            return (val & bit) * 0x10000000 + (low & lowbit);
        }
        return (val & bit);
    }

    function bitor(val, bit) {
        if (bit > 0xFFFFFFF || val > 0xFFFFFFF) {
            var low = val & 0xFFFFFFF;
            var lowbit = bit & 0xFFFFFFF;
            val /= 0x10000000;
            bit /= 0x10000000;
            return (val | bit) * 0x10000000 + (low | lowbit);
        }
        return (val | bit);
    }

Cheers, Jason 干杯,杰森

It's not really your browser that's the issue but the language specification. 真正的问题不是您的浏览器,而是语言规范。 In short.. the logical bitwise operators &, | 简而言之..逻辑按位运算符&,| etc all treat their operands as 32-bit integers: 等等都将其操作数视为32位整数:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

In order to do 64-bit wide operation you're going to have to rely on a library or write your own function. 为了执行64位宽的操作,您将不得不依赖库或编写自己的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM