问题描述
我有等效的c ++,但不确定如何在javascript / nodejs中实现它。
c ++:
template <unsigned int BITS>
enum { WIDTH = BITS / 32 };
uint32_t pn[WIDTH];
uint256 seed = "00000800ab9d2c5409a9b4dea2aa6f8471cecc41b35706e6d6155098e5f3595d";
uint64_t Get64(int n = 0) const
{
return pn[2 * n] | (uint64_t)pn[2 * n + 1] << 32;
}
uint64_t first = seed.Get64(0) % 6 + 1;
uint64_t second = seed.Get64(1) % 6 + 1;
首先可以使用以下命令获取uint64_t。但不知道如何在第二个实施中实现。
//uint64_t first = seed.Get64(0) % 6 + 1;
var bigInt = require("big-integer");
var hash = bigInt("00000800ab9d2c5409a9b4dea2aa6f8471cecc41b35706e6d6155098e5f3595d",16);
console.log(hash.and(new bigInt("ffffffffffffffff",16)).mod(6) + 1)
//result of first = 6
解决方法
如何使用JavaScript“原生” BigInt
var instances = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.GetInterfaces().Contains(typeof(IMessage))
&& t.GetConstructor(Type.EmptyTypes) != null
select Activator.CreateInstance(t) as IMessage;
foreach (var instance in instances)
{
string json = instance.ToString(); // doesnot work
Console.WriteLine(instance.GetType().GetNestedTypes().ToString()); // doesnot work
}
或者,您可以做很多人认为不可以并扩展BigInt原型的事情-通常我建议扩展BigInt类,但是,据我所知,这是不可能的(因为const get64 = (value,n=0) => (value >> BigInt(64*n)) & 0xffffffffffffffffn;
const toNumber = value => +value.toString();
var hash = 0x00000800ab9d2c5409a9b4dea2aa6f8471cecc41b35706e6d6155098e5f3595dn;
var first = toNumber(get64(hash,0) % 6n + 1n);
var second = toNumber(get64(hash,1) % 6n + 1n);
console.log(first,second)
是不支持)
new BigInt