tags:

views:

106

answers:

3

Hi,

i want to generate a pseudo-random bool stream based on a modulo operation on another stream of integers (say X), so the operation would be

return ( X % 2);

The only problem is that X is a stream of integers that always ends in 1, so for instance would be somehing like 1211, 1221, 1231, 1241 .... is there a way for me to disregard the last bit (without using string manip) so the test doesnt always pass or always fail?

+3  A: 

How about (X / 10) % 2 then?

David M
tried that - i guess because its dropping of the remainder that doesnt work either...
oneAday
Why wouldn't that work? It will work with the stream of numbers in your example.
David M
its strange, its always returning the odd test as true, still.
oneAday
So are you sure the numbers you posted are truly representative?
David M
Obviously not: (1221 / 10) % 2 == 122 % 2 == 0.
MSalters
(1220 /10) % 2 == 122 % 2 == 0 also though (so it doesnt flip)
oneAday
But your numbers end in the digit `1`, so `1220` isn't possible, is it?
Alok
A: 
return ( x%20/10 );
Andrew McGregor
+2  A: 

If you'd otherwise be happy to use the last bits, use the penultimate bits instead:

return (x & 0x2) >> 1;

So say the next number from your stream is 23:

  1 0 1 1 1  // 23 in binary
& 0 0 0 1 0  // 0x2 in binary
-----------
  0 0 0 1 0

Shifting that right by one bit (>> 1) gives 1. With 25, the answer would be 0:

  1 1 0 0 1
& 0 0 0 1 0
-----------
  0 0 0 0 0
Greg Bacon
so convert to binary and then ... sorry, dont quite follow
oneAday
Basically this gives you back the second-last bit.
Georg
Georg
MSalters
ephemient