mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2012-02-08, 23:24   #12
Dubslow
Basketry That Evening!
 
Dubslow's Avatar
 
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88

719710 Posts
Default

This is a great example of the socratic method, as well as a great way to learn binary.
Dubslow is offline   Reply With Quote
Old 2012-02-08, 23:25   #13
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

836910 Posts
Default

Quote:
Originally Posted by Bundu View Post
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 -
science_man_88 is offline   Reply With Quote
Old 2012-02-09, 04:47   #14
axn
 
axn's Avatar
 
Jun 2003

23×19×31 Posts
Default

Quote:
Originally Posted by Bundu View Post
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.
axn is offline   Reply With Quote
Old 2012-02-09, 05:34   #15
Batalov
 
Batalov's Avatar
 
"Serge"
Mar 2008
Phi(4,2^7658614+1)/2

912410 Posts
Default

axn's method is the stripped-to-the-bone implementation of fivemack's meta-method (a bitmask is the smallest hash array, etc etc).
Batalov is offline   Reply With Quote
Old 2012-02-11, 15:38   #16
Bundu
 
Bundu's Avatar
 
Jul 2004
Mid Calder, Scotland

5·37 Posts
Default

Quote:
Originally Posted by axn View Post
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
Bundu is offline   Reply With Quote
Old 2012-02-11, 17:34   #17
axn
 
axn's Avatar
 
Jun 2003

126816 Posts
Default

Quote:
Originally Posted by Bundu View Post
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
axn is offline   Reply With Quote
Old 2012-02-11, 20:07   #18
Bundu
 
Bundu's Avatar
 
Jul 2004
Mid Calder, Scotland

2718 Posts
Default

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....
Bundu is offline   Reply With Quote
Old 2012-02-12, 03:38   #19
Christenson
 
Christenson's Avatar
 
Dec 2010
Monticello

24×107 Posts
Default

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.
Christenson is offline   Reply With Quote
Old 2012-02-15, 20:30   #20
cheesehead
 
cheesehead's Avatar
 
"Richard B. Woods"
Aug 2002
Wisconsin USA

22·3·599 Posts
Default

Quote:
Originally Posted by Bundu View Post
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
cheesehead is offline   Reply With Quote
Old 2012-02-19, 18:09   #21
Bundu
 
Bundu's Avatar
 
Jul 2004
Mid Calder, Scotland

2718 Posts
Default

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
File Type: txt pkrv10.txt (23.9 KB, 67 views)
Bundu is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shift Number in LL Test Kalli Hofmann Information & Answers 1 2018-01-08 12:24
how can I test a number in any prime95? Welton Information & Answers 7 2016-07-29 12:07
P95: Sequential (?!) Stage 2 of P-1? LaurV Software 11 2012-06-28 09:57
What's the largest known sequential prime? Unregistered Homework Help 4 2009-09-11 11:46
Time to test arbitrary number 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

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.