Jump to content




Binary and Computer Logics


  • You cannot reply to this topic
33 replies to this topic

#21 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 May 2013 - 08:55 PM

View PostEngineer, on 05 May 2013 - 04:28 PM, said:

This saves you a lot on time on skype ^_^
haha yeh it does.

#22 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 05 May 2013 - 10:25 PM

i learned base conversons by myself with my calculator
i was trying to solve myan math problems (base 20) and didnt want to convert by hand
converting from something to base 10 is easy, you just loop through the number and do this:
C+(N*(B^I)) -> C
C = result, N = NUM[I+1], B = base, I = iterator (starts at 0)
so, every number in NUM is worth (multiplied) by the base raised by the iterator
converting to a base is a bit trickier, this time in LUA:
local O={}
local N=100
local B=20
repeat
local D=(N%B)/>/>+1
N=math.floor(N/B)/>/>
table.insert(O,1,D)
until N==0
as you can see i use modulous instead of getting the remainder of the division
if you are doing this on paper just use subtraction, unless you are really good at long division

#23 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 May 2013 - 10:34 PM

View PostPixelToast, on 05 May 2013 - 10:25 PM, said:

converting from something to base 10 is easy
I would say that converting a base to base-2 and base-10 are the easiest of the lot, which is why they are the common ones that are used as the middle step to go from one base to another (where either side doesn't include base-2 or base-10)

View PostPixelToast, on 05 May 2013 - 10:25 PM, said:

unless you are really good at long division
Oh god, long division. One major thing in life that I hate and have never been good at.

#24 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 05 May 2013 - 11:03 PM

View Posttheoriginalbit, on 05 May 2013 - 03:19 PM, said:

well I think it wouldn't be too confusing to the beginner if I detailed the long and lazy way (the way I actually do it, since most my math I do in my head and this way is easier, and I'm generally very lazy)
<source-base> —> <base-10> —> <target-base>
But the hard and short way is more efficient in programming and more flexible than the long and lazy way

#25 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 May 2013 - 11:08 PM

View PostKingOfNoobs, on 05 May 2013 - 11:03 PM, said:

But the hard and short way is more efficient in programming and more flexible than the long and lazy way
Oh definitely. When/if I do/get around to doing a coding section I'll be sure to use the short way. But I think for ease of explaining and to try and keep it at a beginner-ish level I'm not going to explain that way. Unless you can think of a way to explain it well but still keep it easy to understand.

#26 Symmetryc

  • Members
  • 434 posts

Posted 08 May 2013 - 09:28 PM

I have another question :P. Where do you plan on going with this? Making Adders, ALUs, CPUs, etc? Or are you just trying to inform people about Boolean Algebra and Bases? :P

#27 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 08 May 2013 - 11:07 PM

View PostSymmetryc, on 08 May 2013 - 09:28 PM, said:

I have another question :P. Where do you plan on going with this? Making Adders, ALUs, CPUs, etc? Or are you just trying to inform people about Boolean Algebra and Bases? :P
I don't know why you would want to make a program for Adders, etc, so no it will not be for that. Once I get to updating it and finishing off the theory part of it, I will give examples of how we can use the knowledge just learnt to make more efficient programs and how we can use things like bitwise AND, OR and NOT... I will be updating as soon as I can get this damn backlog of uni assignments complete.

#28 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 09 May 2013 - 02:17 AM

Hey,I just made a simple function to convert numbers from any base to any other base between 2 and 16 because I was bored and I just thought I could post this in this thread. The syntax is: convert(from which base do you want to convert, to which base do you want to convert, table with all characters(as strings) of the number separately you want to convert from). The output is a table with all characters as strings in it. Here is the code:
Spoiler
Also I should say that I only have tested it in codea(an app for iPad for making little games or simulations in lua) but not yet in computercraft. You're all of course free to use this wherever you want(I don't even want credit, but it would still be nice). Also it's still possible to use numbers that aren't in the number system you come from (like using A in binary) and its also possible to put base numbers in that don't exist or aren't supported(like base -10 or base 18). And one last thing: it can only convert integers(123 would go, 12.3 not)

#29 Symmetryc

  • Members
  • 434 posts

Posted 10 May 2013 - 05:26 PM

I've just dug up one that I made a while ago, it's a bit more compact and supports up to base 36:
Spoiler


#30 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 11 May 2013 - 06:01 AM

View PostSymmetryc, on 10 May 2013 - 05:26 PM, said:

I've just dug up one that I made a while ago, it's a bit more compact and supports up to base 36:
Spoiler
i have to say this is way better than mine

#31 Symmetryc

  • Members
  • 434 posts

Posted 11 May 2013 - 06:21 AM

View PostKingOfNoobs, on 11 May 2013 - 06:01 AM, said:

View PostSymmetryc, on 10 May 2013 - 05:26 PM, said:

I've just dug up one that I made a while ago, it's a bit more compact and supports up to base 36:
Spoiler
i have to say this is way better than mine
Nah, I think they're pretty similar, except I used a string and 'subbed it to convert. If I remember correctly, when I was making this I found a psuedoalgorithm on a forum that was written in some form of BASIC that changed Hex to Binary and then I rewrote it in Lua and modified it so that it could change from any base to any base. If I found the link I would credit them, but I couldn't, I only found the function I made from his.

Edit: I realized that there was a huge flaw in my code :P. It couldn't handle non-string inputs, even if they didn't include and non-decimal numbers, so I added a couple of tostring()'s here and there. And, while doing that, I realized I could easily expand it to up to base 64, so I did and it's still the same amount of lines as before :P (albeit with a ridiculously long string that is used for the different base numbers).
Spoiler


#32 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 11 May 2013 - 07:12 AM

View PostSymmetryc, on 11 May 2013 - 06:21 AM, said:

View PostKingOfNoobs, on 11 May 2013 - 06:01 AM, said:

View PostSymmetryc, on 10 May 2013 - 05:26 PM, said:

I've just dug up one that I made a while ago, it's a bit more compact and supports up to base 36:
Spoiler
i have to say this is way better than mine
Nah, I think they're pretty similar, except I used a string and 'subbed it to convert. If I remember correctly, when I was making this I found a psuedoalgorithm on a forum that was written in some form of BASIC that changed Hex to Binary and then I rewrote it in Lua and modified it so that it could change from any base to any base. If I found the link I would credit them, but I couldn't, I only found the function I made from his.

Edit: I realized that there was a huge flaw in my code :P/>. It couldn't handle non-string inputs, even if they didn't include and non-decimal numbers, so I added a couple of tostring()'s here and there. And, while doing that, I realized I could easily expand it to up to base 64, so I did and it's still the same amount of lines as before :P/> (albeit with a ridiculously long string that is used for the different base numbers).
Spoiler
Btw where is this stuff like anystring:sub() documented? I didn't find it in the official lua documentation. That's also the reason why i made it with tables instead of strings(i just didn't knew how to use this stuff)

#33 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 11 May 2013 - 07:53 AM

View PostKingOfNoobs, on 11 May 2013 - 07:12 AM, said:

Btw where is this stuff like anystring:sub() documented? I didn't find it in the official lua documentation. That's also the reason why i made it with tables instead of strings(i just didn't knew how to use this stuff)
http://lua-users.org...LibraryTutorial
most of the Lua APIs you can use object notation on. :)

#34 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 11 May 2013 - 08:14 AM

View Posttheoriginalbit, on 11 May 2013 - 07:53 AM, said:

View PostKingOfNoobs, on 11 May 2013 - 07:12 AM, said:

Btw where is this stuff like anystring:sub() documented? I didn't find it in the official lua documentation. That's also the reason why i made it with tables instead of strings(i just didn't knew how to use this stuff)
http://lua-users.org...LibraryTutorial
most of the Lua APIs you can use object notation on. :)/>
Thanks





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users