mersenneforum.org sequential number test
 Register FAQ Search Today's Posts Mark Forums Read

 2012-02-08, 23:24 #12 Dubslow Basketry That Evening!     "Bunslow the Bold" Jun 2011 40
2012-02-08, 23:25   #13
science_man_88

"Forget I exist"
Jul 2009
Dumbassville

836910 Posts

Quote:
 Originally Posted by Bundu Lol, after checking the link I realised I had already seen this and knew it I needed something even MORE basic and began here! http://www.math.grin.edu/~rebelsky/C...student-binary Edit: Hooray for base 2!
though I didn't ever go far I tried learning javascript from: http://www.pageresource.com/jscript/index4.htm once

Quote:
 What you need to use JavaScript Basic Recognition of a JavaScript onMouseover: Your first Script Using Buttons for JavaScripts Forward and Back Buttons JavaScript Alerts Variables and Functions Logical Operators JavaScript Prompts Password Protection Password Protection 2 Confirmation Boxes JS Browser Detection JS Redirection Opening a New Window JavaScript Windows - By Andy Scott Using Link Tags For Scripts The setTimeout Function JavaScript Arrays JavaScript Associative Arrays Preloading Images Hover Buttons JS Hover Buttons 2 JS Hover Buttons 3 JS Rollovers: 4 JavaScript Object Detection JavaScript and Frames Change 2 Frames at Once Beginning With Forms Navigation Drop Boxes Drop Boxes- No Button Drop Boxes with Frames More JavaScript Buttons External JavaScripts JavaScript Clocks JS Clock 2 The Math Object The Random Function Strings with charAt and indexOf Strings 2: The Split Method Get the Viewer's Screen Resolution Y2K and Your Scripts -

2012-02-09, 04:47   #14
axn

Jun 2003

23×19×31 Posts

Quote:
 Originally Posted by Bundu It is so good it doesn't even need the array to be sorted, is that intentional or a by product of the bitwise operation?
Both. One of the reasons for selecting the bitwise approach was to avoid the sort.

 2012-02-09, 05:34 #15 Batalov     "Serge" Mar 2008 Phi(4,2^7658614+1)/2 912410 Posts axn's method is the stripped-to-the-bone implementation of fivemack's meta-method (a bitmask is the smallest hash array, etc etc).
2012-02-11, 15:38   #16
Bundu

Jul 2004
Mid Calder, Scotland

5·37 Posts

Quote:
 Originally Posted by axn It will detect it correctly as a straight. The repeated application of "|" (or) operator on the same bit will not cause any problem. EDIT:- Sample code Code: function is_straight(v) { // v is a vector of card values var x = 0; var i; for(i=0; i < 7; i++) { x |= (1 << v[i]); // v[i] is assumed to be between 1 and 13, where 1 = 'A' and 13 = 'K' if (v[i] == 1) // special check for Ace x |= (1 << 14); } x &= (x << 1) & (x << 2) & (x << 3) & (x << 4); return (x > 0); // return result } call with "is_straight([1,2,3,4,5,6,7])"
Ok axn. I attempting to understand this function and to be honest I am struggling so I just wanted to check my understanding so far if that is ok? From what I have read so far the << operator performs a bitshift to the right equal to the operand on the right.

Bitshifting left is raises by a power of 2?

So 1<<1: would be 00000001 = 1
So 1<<2: becoming 00000010 = 2
So 1<<3: becoming 00000100 = 4
So 1<<4: becoming 00001000 = 8

So 3<<1: would be 00000011 = 3
So 3<<2: becoming 00000110 = 6
So 3<<3: becoming 00001100 = 12
So 3<<4: becoming 00011000 = 24

Now looking at the formula am I correct in understanding that within the for loop (x |= (1 << v[i]);) you are bitshifting 1 by the numerical value of each card in the array? and if there is an ace present (v[i] == 1) a further (repeat?) bitshift is done to the value of 14

so if the array is [1,2,3,4,5,6,7]

then we get

00000001
00000010
00000100
00001000
00010000
00100000
01000000
and since there is an ace present 10000000000000 ?

Now the tutorial I read on binary was dealing with 8 bit strings but modern computers use 32? So would it be more correct to say the above is actually

00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000010
00000000 00000000 00000000 00000100
00000000 00000000 00000000 00001000
00000000 00000000 00000000 00010000
00000000 00000000 00000000 00100000
00000000 00000000 00000000 01000000
00000000 00000000 00100000 00000000

So the result of the for loop is series of binary strings which will then be looked at by this:

x &= (x << 1) & (x << 2) & (x << 3) & (x << 4);

return (x > 0);

and will give us a TRUE/FALSE result if there is a numerical sequence present?

Hopefully I am correct so far ( I haven't quite figured out the last bit

x &= (x << 1) & (x << 2) & (x << 3) & (x << 4);

Yet but given a little more study I am hoping I will get it!

EDIT: Looking at this a little more, I was wondering if I have made an error in my assumption that there are multiple binary strings? I did think that the for loop result would be:

00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000010
00000000 00000000 00000000 00000100
00000000 00000000 00000000 00001000
00000000 00000000 00000000 00010000
00000000 00000000 00000000 00100000
00000000 00000000 00000000 01000000
00000000 00000000 00100000 00000000

but is it actually:

00000000 00000000 00100000 01111111 ?

Last fiddled with by Bundu on 2012-02-11 at 16:14

2012-02-11, 17:34   #17
axn

Jun 2003

126816 Posts

Quote:
 Originally Posted by Bundu but is it actually: 00000000 00000000 00100000 01111111 ?
Yes.

Code:
x |= (1 << v[i]);
is short hand for
Code:
x = x | (1 << v[i]);
So, all the (1 << v[i]) are being OR'ed together in x.

Then compute:
x ____ : 00000000 00000000 00100000 01111111
x << 1 : 00000000 00000000 01000000 11111110
x << 2 : 00000000 00000000 10000001 11111100
x << 3 : 00000000 00000001 00000011 11111000
x << 4 : 00000000 00000010 00000111 11110000

Then AND them all together (Again... x &= y is shorthand for x = x & y). See if there are any '1' bits left in the result. If so, it indicates that there are 5 consecutive '1's in x.

Last fiddled with by axn on 2012-02-11 at 17:35

 2012-02-11, 20:07 #18 Bundu     Jul 2004 Mid Calder, Scotland 2718 Posts Sir you are a legend. I know I am asking a lot of questions, hopefully you won't mind a few more! Ok so longhand x = x & (x << 1) & (x << 2) & (x << 3) & (x << 4); Now I am not 100% certain but I am assuming that we work from left to right in this calculation 00000000 00000000 00100000 01111111 = x &00000000 00000000 01000000 11111110 = (x << 1) Gives 00000000 00000000 00000000 01111110 &00000000 00000000 10000001 11111100 = (x << 2) Gives 00000000 00000000 00000000 01111100 &00000000 00000001 00000011 11111000 = (x << 3) Gives 00000000 00000000 00000000 01111000 &00000000 00000010 00000111 11110000 = (x << 4) Final Result 00000000 00000000 00000000 01110000 so since the final result is >0 we get a true and we have a straight! [2,4,6,8,10,12,14] 00000000 00000000 00101010 10101010 = x 00000000 00000000 01010101 01010100 = (x << 1) 00000000 00000000 10101010 10101000 = (x << 2) 00000000 00000001 01010101 01010000 = (x << 3) 00000000 00000010 10101010 10100000 = (x << 4) 00000000 00000000 00101010 10101010 = x &00000000 00000000 01010101 01010100 = (x << 1) Gives 00000000 00000000 00000000 00000000 &00000000 00000000 10101010 10101000 = (x << 2) Gives 00000000 00000000 00000000 00000000 &00000000 00000001 01010101 01010000 = (x << 3) Gives 00000000 00000000 00000000 00000000 &00000000 00000010 10101010 10100000 = (x << 4) Final Result 00000000 00000000 00000000 00000000 so since the final result is not >0 we get a false and we do not have a straight! Quite beautiful really. Which leads me to assume/understand: a) the reason sorting is not required is becuase this happens during the |- for loop (in a way) when the bits are distributed by shifting? b) repeated numbers become a non issue again becuase the for loop OR sequence sets the flag to 1 the first time the number occurs and then keeps it set any time a repeat occurs c) this formula could be modified to look for longer or shorter sequences quite *easily* so if I wanted to check 5 cards for a seqence of 3 numbers then Code: function is_shortstraight(v) { // v is a vector of card values var x = 0; var i; for(i=0; i < 5; i++) { x |= (1 << v[i]); // v[i] is assumed to be between 1 and 13, where 1 = 'A' and 13 = 'K' if (v[i] == 1) // special check for Ace x |= (1 << 14); } x &= (x << 1) & (x << 2); return (x > 0); // return result } or to check 9 cards for a sequence of 6 Code: function is_longstraight(v) { // v is a vector of card values var x = 0; var i; for(i=0; i < 9; i++) { x |= (1 << v[i]); // v[i] is assumed to be between 1 and 13, where 1 = 'A' and 13 = 'K' if (v[i] == 1) // special check for Ace x |= (1 << 14); } x &= (x << 1) & (x << 2) & (x << 3) & (x << 4) & (x << 5); return (x > 0); // return result } Last fiddled with by Bundu on 2012-02-11 at 20:08 Reason: If only we could edit the things we say as easily, my wife would never be in a huff with me....
 2012-02-12, 03:38 #19 Christenson     Dec 2010 Monticello 24×107 Posts As an in-practice programmer, if I have to know that the associativity of an operation goes in a particular direction in order to get the right result, then the code is unclear and gets parentheses. What has happened to your sorting is that since there are only so many boxes to put things in, and you set a bit in the box when you find something, you ARE sorting...you just don't know that it is, since the standard sorting algorithms are significantly more general.
2012-02-15, 20:30   #20

"Richard B. Woods"
Aug 2002
Wisconsin USA

22·3·599 Posts

Quote:
 Originally Posted by Bundu From what I have read so far the << operator performs a bitshift to the right equal to the operand on the right.
Not exactly.

"<<" is a bitshift to the left. ">>" is a bitshift to the right.

Quote:
 Bitshifting left is raises by a power of 2?
Yes

Quote:
 So 1<<1: would be 00000001 = 1 So 1<<2: becoming 00000010 = 2 So 1<<3: becoming 00000100 = 4 So 1<<4: becoming 00001000 = 8
No, all those are off-by-1 in the shift count.

Should be:

1<<1: would be 00000010 = 2 = multiplication by 2^1

and so forth ...

But you do have the shifts correct in your second post. :-)

Last fiddled with by cheesehead on 2012-02-15 at 20:34

2012-02-19, 18:09   #21
Bundu

Jul 2004
Mid Calder, Scotland

2718 Posts

Well thanks to everyone's help I have managed to complete the first part of my work which is hand evaluation! I know it isn't the prettiest code in the world but it works, I coded it myself almost entirely and I am pretty happy with it so far. No doubt as my skills improve I will come back and improve it over time. But for now at least I can tackle the next section which is player interaction and betting!

If anyone is interested the attached text file is what I have so far, simply save as an html and bash away at the refresh button :-)
Attached Files
 pkrv10.txt (23.9 KB, 67 views)

 Similar Threads Thread Thread Starter Forum Replies Last Post Kalli Hofmann Information & Answers 1 2018-01-08 12:24 Welton Information & Answers 7 2016-07-29 12:07 LaurV Software 11 2012-06-28 09:57 Unregistered Homework Help 4 2009-09-11 11:46 JuanTutors Math 3 2007-05-16 12:13

All times are UTC. The time now is 19:02.

Sat Oct 24 19:02:33 UTC 2020 up 44 days, 16:13, 0 users, load averages: 1.78, 1.63, 1.76