OCaml字节码的可移植性(Portability of OCaml bytecode)
我在x86机器上用字节码编译OCaml程序,并将字节码传送到ppc64机器。 假设ppc64机器有ocamlrun(编译为ppc64),我能在ppc64架构上执行我的程序吗? 这么简单吗?
换句话说,字节码是否符合字节序?
如果没有,那么字节码的目的是什么?
I compile an OCaml program in bytecode on an x86 machine, and transfer the bytecode to a ppc64 machine. Assuming that the ppc64 machine has ocamlrun (compiled for ppc64), will I be able to execute my program on the ppc64 architecture ? Is it that simple ?
In other terms, is the bytecode resistant to endianness ?
If not, then what is the purpose of the bytecode ?
原文:https://stackoverflow.com/questions/34814010
最满意答案
根据规范 ,在
"12345" >> 0
,会发生这种情况:
"12345"
通过规范的抽象ToInt32
函数 :
- ToInt32通过
ToNumber
运行它以将字符串强制转换为标准JavaScript号(IEEE-754双精度二进制浮点值); 它最终成为12345ToInt32
然后将其转换为32位整数值; 它最终成为...... 12345- 这个32位整数被移位......完全没有,因为你使用了
>> 0
,所以我们仍然有12345并将返回一个int
有点。 如果将结果存储在变量中,它最终将再次成为标准JavaScript编号(双精度)。 如果将这个操作与其他操作相结合,那么规范似乎允许将int值原样使用,这是有道理的。
如果您的目标只是使用默认的强制规则强制
"12345"
为数字,则>> 0
是一种相当模糊且有些低效的方法(不是后者可能很重要)。 一元+
更直接并适用相同的规则:+"12345"
According to the specification, in
"12345" >> 0
, this happens:
"12345"
is put through the specification's abstractToInt32
function:
- ToInt32 runs it through
ToNumber
to coerce the string to a standard JavaScript number (an IEEE-754 double-precision binary floating-point value); it ends up being 12345ToInt32
then converts it to a 32-bit integer value; it ends up being...12345- That 32-bit integer is shifted...not at all because you used
>> 0
, so we still have 12345and will return a int
Sort of. If you stored the result in a variable, it would end up being a standard JavaScript number again (a double). The spec seems to allow for the possibility the int value would be used as-is if you combined this operation with others, though, which makes sense.
If your goal is just to force
"12345"
to be a number using the default coercion rules,>> 0
is a fairly obscure and somewhat inefficient way to do it (not that the latter is likely to matter). The unary+
is rather more direct and applies the same rules:+"12345"
相关问答
更多-
优化Bitshift成阵列(Optimizing Bitshift into Array)[2022-07-11]
你肯定做的工作超出了你的需要。 注意函数ReverseBits()如何努力将反转字的字节按正确的顺序排列,以及下一个发生的事情 - 你将减速的部分归因于 - 重新排序那些相同的字节。 您可以编写并使用ReverseBits()的修改版本, ReverseBits()反转表示的字节直接放入数组中的正确位置,而不是将它们打包成整数,只是为了再次解压缩它们。 这应该至少要快一点,因为你会严格删除操作。 You're certainly doing more work than you need to do. No ... -
麻烦与转移操作(Trouble with bitshift operations)[2021-07-07]
public static string ToHex(byte[] bytes) { char[] c = new char[bytes.Length * 2]; byte b; for (int bx = 0, cx = c.Length - 1; bx < bytes.Length; ++bx) { b = ((byte)(bytes[bx] & 0x0F)); c[cx--] = (char)(b > 9 ? b + 0x37 + 0x2 ... -
你所看到的可能是算术位移 。 当向右移动时,最左边的位(通常是有符号整数表示中的符号位)被复制以填充所有空置位置(这是一种符号扩展)。 C99标准 6.5.7§5说: E1 >> E2的结果是E1右移E2位的位置。 [...]如果E1有签名类型和负值,则结果值是实现定义的。 所以结果可能是编译器作者决定的任何东西。 他们可能决定扩展符号位,编译器文档应该提及它。 What you are seeing is probably arithmetic bit shift. when shifting to th ...
-
根据规范 ,在"12345" >> 0 ,会发生这种情况: "12345"通过规范的抽象ToInt32函数 : ToInt32通过ToNumber运行它以将字符串强制转换为标准JavaScript号(IEEE-754双精度二进制浮点值); 它最终成为12345 ToInt32然后将其转换为32位整数值; 它最终成为...... 12345 这个32位整数被移位......完全没有,因为你使用了>> 0 ,所以我们仍然有12345 并将返回一个int 有点。 如果将结果存储在变量中,它最终将再次成为标准Jav ...
-
16^(n - i -1) = 2^(4 * (n - i -1)) 2^x = 1 << x. 因此: 16^(ni-1) = 1 << (4 * (n -i -1)) (使用^符号表示“对于权力”,而不是XOR) 16^(n - i -1) = 2^(4 * (n - i -1)) 2^x = 1 << x. Therefore: 16^(n-i-1) = 1 << (4 * (n -i -1)) (Using the ^ symbol to mean "to the power of", not XO ...
-
对于您显示的枚举,以下内容将起作用: int dbValue = ...; var status = (Status)dbValue; 如果数据库值只能取三个值中的一个 - 0(活动),1(空白),2(退款) - 则可能更清楚地使您的枚举具有以下等效值。 public enum Status { Active = 0, Voided = 1, Refunded = 2 } 你使用的位移建议一个位字段,但这不是你实际处理的。 拥有Status.Voided | Status.Ref ...
-
在JavaScript find()polyfill中使用bitshift运算符(Use of bitshift operator in JavaScript find() polyfill)[2023-04-04]
我知道它有时被用作将浮点值截断为整数的丑陋方式 不完全是。 value >>> 0是使用ToUint32(value)抽象操作的唯一公开方式。 数组具有不变量,即它们的长度必须是Uint32值: 每个Array对象都有一个length属性,其值始终是小于2 32的非负整数。 这就是为什么规范在数组方法定义中使用ToUint32的原因。 Polyfill使用>>> 0而不是ToUint32,因为ToUint32没有暴露给JS代码。 您需要使用ToUint32,因为您无法知道它在数组方法中的含义,它可以是非数组 ... -
Bitshift Char Array(Bitshift Char Array)[2022-05-24]
如果你的意思是一个实际的char数组,你可以使用memmove()和memset() : char str[] = "0000000101"; int shift = 3; int length = strlen(str); memmove(str, str + shift,length - shift); memset(str + length - shift,'0',shift); // Result: // "0000101000" If you meant an actual char ... -
charCodeAt函数接受一个索引,而不是一个字符串。 当你传递一个字符串时,它会尝试将字符串转换为数字,如果不能,则使用0 。 你的第一次迭代调用'1'.charCodeAt('1') 。 它会将'1'解析为一个数字并尝试获取字符串中的第二个字符代码。 由于字符串只有一个字符,这就是NaN 。 你的第二次迭代调用'f'.charCodeAt('f') 。 由于'f'不能被解析为一个数字,它会被解释为0 ,这会给你第一个字符代码。 您应该编写var hexval = result.charCodeAt(i ...
-
这是使用系数0.59和0.11将24位RGB值转换为灰度值的技巧(请注意,这些值加起来为1 )。 此操作(rgbValue & 0xFF0000) >>> 16将位17..24切除,并将它们右移到位置0..7,产生0到255之间的值,包括0和255。 类似地, (rgbValue & 0xFF00) >>> 8切出位8..16,并将它们移位到位置0..7。 本问答讨论了系数,并讨论了其他替代方案。 This is the trick that converts a 24-bit RGB value to a ...