Z
zendraw
Guest
why is this returning me 0 sometimes? 2/3 times
krypt=1<<irandom_range(2, 100);
show_debug_message(krypt);
krypt=1<<irandom_range(2, 100);
show_debug_message(krypt);
It wouldn't necessarily be platform dependent, but CPU-dependent. However, every CPU I'm familiar with has different instructions for bit-shifting (x86 uses SHR/SHL) and bit-rotating (ROR/ROL). The latter wraps around bits. The former drops bits and usually sets a carry flag, and that is what GM uses when you use the >>/<< operators.That behavior seems like it might be platform dependent actually. But the reasoning for getting a zero there makes sense to me.
for (var _i = 0; _i < 100; _i++) {
show_debug_message (string(_i) + " = " + string (1 << _i) );
}
[...]
58 = 288230376151711744
59 = 576460752303423488
60 = 1152921504606846976
61 = 2305843009213693952
62 = 4611686018427387904
63 = -9223372036854775808
64 = 0
65 = 0
66 = 0
67 = 0
68 = 0
69 = 0
[...]
show_debug_message(string(64) + " = " + string(1 << 64));
64 = 1
You seem more familiar with the specifics than me. I know enough to recognize the idea that it is a low-level enough operation to be affected by different platforms, and yes it makes sense that different CPUs would handle it differently too.It wouldn't necessarily be platform dependent, but CPU-dependent. However, every CPU I'm familiar with has different instructions for bit-shifting (x86 uses SHR/SHL) and bit-rotating (ROR/ROL). The latter wraps around bits. The former drops bits and usually sets a carry flag, and that is what GM uses when you use the >>/<< operators.
At the least, this proves what happens on the specific platform/CPU that Roldy is using.Well thankfully we have a tool called a computer that lets us answer these questions:
GM using the platform equivalent of ROR/L instead of SHR/L depending on platform would be a massive bug that would need to be fixed immediately. All CPUs I'm aware of for platforms GM supports have dedicated rotate (wrap) instructions. GM bit shift operators do not do that, they do simple arithmetic shifts. If it did otherwise, it would be just as fatal an error as dividing instead of multiplying.You seem more familiar with the specifics than me. I know enough to recognize the idea that it is a low-level enough operation to be affected by different platforms, and yes it makes sense that different CPUs would handle it differently too.
The 'platform' is irrelevant to the definition. GML operators are logically defined by the language, and the output will be the same independent of hardware or OS. How they get implemented on hardware is irrelevant as long as it implements the language definition. If it doesn't then the implementation is incorrect.At the least, this proves what happens on the specific platform/CPU that Roldy is using.
You are 100% right about this. There are a few documented differences in the platforms though, and I'm sure there are a few that aren't documented too.The 'platform' is irrelevant to the definition. GML operators are logically defined by the language, and the output will be the same independent of hardware or OS. How they get implemented on hardware is irrelevant as long as it implements the language definition. If it doesn't then the implementation is incorrect.
var data = argument0;
var c = 0;
var dir = sign(argument1);
repeat abs(argument1) {
if dir > 0 {
c = data[@0] >> 63;
data[@0] = data[@0] << 1;
data[@1] = data[@1] << 1 | c;
}
else {
c = data[@1] << 63;
data[@1] = data[@1] >> 1;
data[@0] = data[@0] >> 1 | c;
}
}
the 2 is so we dont have a 1 and a 0 as a value, the second i dont understand, are you trying to bypass the bitshift falloff thing?Am I the only one still curious as to why we even need a value of 2 to the 100th power generated randomly?
Once you get the odd rollover bug sorted out, you just need to make krypt an array and pass it to a custom script that can carry bits.
Code:var data = argument0; var c = 0; var dir = sign(argument1); repeat abs(argument1) { if dir > 0 { c = data[@0] >> 63; data[@0] = data[@0] << 1; data[@1] = data[@1] << 1 | c; } else { c = data[@1] << 63; data[@1] = data[@1] >> 1; data[@0] = data[@0] >> 1 | c; } }
Doesn't work properly using your method when GM's variables are 64-bit signed. 40% of the time it will exceed the limits of the variable and you get the same value (0x00), 1% you'll get a huge negative nunber, and the rest works as you expect.its about having variations. basically value 100 variations.
var r = irandom_range(2,100);
if r < 64
krypt[0] = 1<<r;
else
krypt[1] = 1<<(r-64);
//In this case, you'd need to pass the randomly generated number as argument0
var i = argument0 >> 6;
var n = argument0 & $3F;
krypt[@n] = 1 << n;
100 variations of what?its about having variations. basically value 100 variations.
thats just wraping around the bit, you end up again with same number of values. the point is to have more then 62 values based on bits. which is not possible here.If you want more than 64 variations (I've had mixed luck with the highest bit being set), you'll need to turn whatever variable you are using into an array and set the higher bits inside the higher indices.
orCode:var r = irandom_range(2,100); if r < 64 krypt[0] = 1<<r; else krypt[1] = 1<<(r-64);
Code://In this case, you'd need to pass the randomly generated number as argument0 var i = argument0 >> 6; var n = argument0 & $3F; krypt[@n] = 1 << n;
The code I posted earlier in this thread was for implementing ROL and ROR subroutines.
just look it as value1=red, value2=blue etc. but instead of red we have 32, 64,128,256 and so on.100 variations of what?
The function call irandom_range(1, 100) alone will give you 100 different numbers. Why do you what powers of 2? If you want only even numbers you can just multiply by 2 once.
If he wants a bitfield then that is what he wants.You have more than 62 colors that not only need to be distinguished, but also selectable in every possible combination?
In my experience, "if [x] wants [y]" rarely means "[y] is the appropriate tool for the purpose [x] is using it" and usually means "[x] heard about this cool thing called [y] and is determined to cram it into something, even if [x] has no idea what a [y] is." As it's said, we so often think about whether we can, but we don't stop often enough to think about whether we should.If he wants a bitfield then that is what he wants.
no, that is not wrapping a bit, that is turning a 64-bit value into a 128-bit value, which is precisely what you would want if you want more than 64 unique values saved into "one variable". This is the same way they handle 32-bit data on 8-bit systems, and the same applies here.thats just wraping around the bit, you end up again with same number of values. the point is to have more then 62 values based on bits. which is not possible here.