[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Even/Odd comparison
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Even/Odd comparison

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by tobiassjosten   Sweden  (79 posts)  [Biography] bio
Date Mon 30 May 2005 10:38 AM (UTC)
Message
I can't seem to find a way to check wether a number is odd or even. I've tried with type() (which just gives 'number') and string.find(tostring(number), "."). String.find didn't prove very helpful, despite being my best bet. :/ I tried escaping the dot (is it in regexp format?) but it didn't work either. Anyone got any suggestions as to how I could do this?

The purpose is to get this function to output a centered text:
function visLine(msg, color)
    lnum, rnum = (74 - string.len(msg)) / 2, (74 - string.len(msg)) / 2
    if not string.find(tostring(lnum), "\.") == false then rnum = lnum + 2 end
    ColourTell("lightgrey", "", string.rep("-",lnum) .. " [ ")
    ColourTell(color, "", msg)
    ColourNote("lightgrey", "", " ] " .. string.rep("-",rnum))
end

Simplicity is Divine | http://nogfx.org/
[Go to top] top

Posted by Larkin   (278 posts)  [Biography] bio
Date Reply #1 on Mon 30 May 2005 02:28 PM (UTC)
Message
Use the math.mod function to find the remainder of your number from dividing by 2. If the result is 0, it's even. Otherwise, it's odd.

if math.mod(num, 2) == 0 then
  print("even")
else
  print("odd")
end
[Go to top] top

Posted by tobiassjosten   Sweden  (79 posts)  [Biography] bio
Date Reply #2 on Mon 30 May 2005 03:53 PM (UTC)
Message
Kind of makes sense to use the math-functions for integers. :P
Thanks alot!

Simplicity is Divine | http://nogfx.org/
[Go to top] top

Posted by Nick Gammon   Australia  (21,321 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Sun 05 Jun 2005 02:42 AM (UTC)
Message
Another approach which doesn't use division is to use the "bit" extension in the MUSHclient Lua implementation.

eg.


isodd = bit.band (x, 1)


Strangely I couldn't find these functions documented anywhere on this site. If someone can find the URL please let me know.

In summary, MUSHclient has a "bit" table which implements bitwise operations, for use in cases like this.


bit.shr

Shift right - shift arg1 right by arg2 bits

eg.


print (bit.shr (64, 2))  --> 16



bit.shl

Shift left - shift arg1 left by arg2 bits

eg.


print (bit.shl (64, 2))  --> 256




bit.band

Bitwise and ("and" is a keyword) - takes multiple arguments

eg.


print (bit.band (15, 7, 3))  --> 3



bit.bor

Bitwise or ("or" is keyword) - takes multiple arguments

eg.


print (bit.bor (1, 4, 8))  --> 13



bit.xor

Exclusive or - takes multiple arguments

eg.


print (bit.xor (15, 1))  --> 14



bit.neg

Bitwise negate (ones complement as a 32-bit unsigned number)

eg.


print (bit.neg (15))  --> 4294967280


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Ked   Russia  (524 posts)  [Biography] bio
Date Reply #4 on Sun 05 Jun 2005 06:37 AM (UTC)
Message
The post with the introduction of Lua bit functions is at:

http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=4907

[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


11,662 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]