This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Engineer's profile picture

Binary API

Started by Engineer, 28 May 2013 - 07:49 AM
Engineer #1
Posted 28 May 2013 - 09:49 AM
Hello all!

Why I wanted to make this API, is because I didnt get, why the bit API has no support for binary input. So that is why I made the API.

The documentation and code is on github. But, you can download it via pastebin.

I think this API is pretty accurate, but it could have some potential flaws. Please report those on the github or here. Also, if you want support for a certain gate, or something else that has to due with binary, please post it here!

And I should mention its pretty strict, you can only put in bytes, just because it makes sense. But everything returns in bytes, so if you dont mutate it yourself, you are food.

Thank you for reading :P/>

Edit: I want to state that this was more a practise than something that could be used practically. You can rather use the bit API, since it is probably way quicker, and this is just a way around it. It was fun to make though.
Edited on 31 May 2013 - 05:41 PM
superaxander #2
Posted 28 May 2013 - 10:02 AM
Very nice! I'll see if I can use it somewhere
ArchAngel075 #3
Posted 31 May 2013 - 03:22 AM
awhile back a driend of mine asked me to make an program that can convert any number into its bineey representation and output the bases used to gain it.
ie
for 3 we get
bases : 1;2
and sequence is 11

its pretty damn fast (123123123) in less than 5s
id be happy to post it for u to use this afternoon. I was wondering where a use for it comes lol

but i do apologize if what ylyour API does is not what my program does.
Engineer #4
Posted 31 May 2013 - 03:49 AM
for 3 we get
bases : 1;2
and sequence is 11

If you use the toBinary function, it should give back: "0011" wich is three. Or I dont get what you are doing, or my API is broken for some reason..
Oh, and Im storing it in a string because lua cant store binary in integers, wich is quite annoying.


its pretty damn fast (123123123) in less than 5s
Uhm..What do you mean with this? You know that binary is base-2, wich only knows zero's and one's.
Also, the most awesome tutorial I have ever read on binary (I actually only needed it for bit shifting :P/> ): TheOriginalBIT's tutorial

And after thinking about it, I got why the original API doesnt use binary input. Because it makes no sense, you get the same output whatsoever! But this was a great practise to me, and still let me know if something is broken or something similair like that.
theoriginalbit #5
Posted 31 May 2013 - 05:10 AM
You made this quick! Thought you were going to take longer than this.

an program that can convert any number into its bineey representation and output the bases used to gain it.
ie
for 3 we get
bases : 1;2
and sequence is 11
Ummm what? how exactly are you calculating the base of 1 AND 2 for 3? o.O
You do realise a number can only be one base?
The base of 3 is 10, since we count in base-10.
Also 5 seconds is a long time to convert base-10 to base-2. Using the toBinary function that Engineer wrote, 123123123 takes, well an unmeasurable amount of time, the display shows 0 seconds.

Also, the most awesome tutorial I have ever read on binary (I actually only needed it for bit shifting :P/> ): TheOriginalBIT's tutorial
Thanks.
ArchAngel075 #6
Posted 31 May 2013 - 06:32 AM
guhh
i may not actually understand binary at all,

my understanding of binary is a number can be represented in a 01 sequence usimg the number sequence 1,2,4,8,16,32 etc. where each nber in seq can be cound by taking its position in the seq afnd doing output = 2^(n-1)

ie in seq 1,2,4,8 etc the 8 is place 4 thus 2^(4-1) gives 8
as 8 is 4th and the only "base", we get 0001.
if we used 5 it notices that the "bases" needed is 1,4 thus output is 101

is my understanding of binary off? im pretty sure it is as i somewhay self taught myself it from looking at the 1,2,4,8 sequence…

i apologize if i am wrong in anyway.
theoriginalbit #7
Posted 31 May 2013 - 09:20 AM
guhh
i may not actually understand binary at all,

my understanding of binary is a number can be represented in a 01 sequence usimg the number sequence 1,2,4,8,16,32 etc. where each nber in seq can be cound by taking its position in the seq afnd doing output = 2^(n-1)

ie in seq 1,2,4,8 etc the 8 is place 4 thus 2^(4-1) gives 8
as 8 is 4th and the only "base", we get 0001.
if we used 5 it notices that the "bases" needed is 1,4 thus output is 101
ok so your understanding of a binary number is fine-ish (few things that could be fixed), in terms of the numbering (1,2,4,8,16,32……) you're spot on, however your terminology of `base` is wrong. what you're referring to is which bit is on or off. Which is what deems our binary number.
A base is a description of how many symbols we use to represent the numbers.
For example here are some common bases with their characters they can display

base-2 (binary);  0, 1
base-8 (octal); 0, 1, 2, 3, 4, 5, 6, 7
base-10 (decimal); 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
base-16 (hexadecimal); 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
etc, etc, etc....
ShadowedZenith #8
Posted 31 May 2013 - 10:18 AM
guhh
i may not actually understand binary at all,

my understanding of binary is a number can be represented in a 01 sequence usimg the number sequence 1,2,4,8,16,32 etc. where each nber in seq can be cound by taking its position in the seq afnd doing output = 2^(n-1)

ie in seq 1,2,4,8 etc the 8 is place 4 thus 2^(4-1) gives 8
as 8 is 4th and the only "base", we get 0001.
if we used 5 it notices that the "bases" needed is 1,4 thus output is 101
ok so your understanding of a binary number is fine-ish (few things that could be fixed), in terms of the numbering (1,2,4,8,16,32……) you're spot on, however your terminology of `base` is wrong. what you're referring to is which bit is on or off. Which is what deems our binary number.
A base is a description of how many symbols we use to represent the numbers.
For example here are some common bases with their characters they can display

base-2 (binary);  0, 1
base-8 (octal); 0, 1, 2, 3, 4, 5, 6, 7, 8
base-10 (decimal); 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
base-16 (hexadecimal); 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
etc, etc, etc....

I think you miss-typed with Base-8. It's 0-7 :P/>
theoriginalbit #9
Posted 31 May 2013 - 10:22 AM
I think you miss-typed with Base-8. It's 0-7 :P/>
Whoops, yeh that was a typo, thanks for pointing it out.
Engineer #10
Posted 31 May 2013 - 10:54 AM
Also, the most awesome tutorial I have ever read on binary (I actually only needed it for bit shifting :P/> ): TheOriginalBIT's tutorial
Thanks.
I was very serious when I said that. You explained it real nice and easy, and better than my teacher by far. I mean, I never understand addition in binary, but thanks to you, it makes sense. I can say this program is based on your explanation!
ArchAngel075 #11
Posted 31 May 2013 - 10:57 AM
Okay, seeing above i understand now myself.

actually read up on binary from you Bit, and saw here i was wrong about "base".
Anyways thanks.
Symmetryc #12
Posted 31 May 2013 - 04:41 PM
I haven't actually used the API yet, but why does evenOut have print("#1") in it?
Mads #13
Posted 31 May 2013 - 04:46 PM
You use alot of unneccesary code in your functions, where you could just use the functions which you had already made to convert from/to binary.
Engineer #14
Posted 31 May 2013 - 05:04 PM
I haven't actually used the API yet, but why does evenOut have print("#1") in it?
Because I forgot to remove it, thanks!


You use alot of unneccesary code in your functions, where you could just use the functions which you had already made to convert from/to binary.
Im going to take a look for improvement tonight

This night a new version with both Mads and Symmetryc things… I messed uo making this verison. Lets keep it on this night a new version :P/>
Engineer #15
Posted 31 May 2013 - 05:29 PM
You use alot of unneccesary code in your functions, where you could just use the functions which you had already made to convert from/to binary.
I pushed a new update, but I cant see how I could optimize it more. Can you please show some examples?
Symmetryc #16
Posted 31 May 2013 - 07:24 PM
And after thinking about it, I got why the original API doesnt use binary input. Because it makes no sense, you get the same output whatsoever!
I don't quite understand what you mean by this; if you were to use bit.band(100, 1001) you would get 96, but if it was to use binary input, you would get 0.
You use alot of unneccesary code in your functions, where you could just use the functions which you had already made to convert from/to binary.
I pushed a new update, but I cant see how I could optimize it more. Can you please show some examples?
You could use tonumber to shorten toDec:

toDec = function(num)
  return tonumber(num, 2)
end
You could just use multiplication/division to shorten your shifting functions:

lshift = function(num, amount)
  return num*(10^amount)
end
rshift = function(num, amount)
  return math.floor(num/(10^amount))
end
You could use math.floor for band:

band = function(num1, num2)
  num1, num2 = evenOut(num1, num2)
  num3 = ""
  for i=1, #num1 do
	char = math.floor((num1:sub(i, i)+num:sub(i, i))/2)
	num3 = num3..char
  end
  return num3
end
Engineer #17
Posted 31 May 2013 - 07:39 PM
And after thinking about it, I got why the original API doesnt use binary input. Because it makes no sense, you get the same output whatsoever!
I don't quite understand what you mean by this; if you were to use bit.band(100, 1001) you would get 96, but if it was to use binary input, you would get 0.
The point of making this, was me understanding binary. This API takes binary input, and the original API takes decimal input.
I prefer it binary, because you might want to maniplate it in some other way. And I got the same results whatsoever, with the binary and bit api..

You use alot of unneccesary code in your functions, where you could just use the functions which you had already made to convert from/to binary.
I pushed a new update, but I cant see how I could optimize it more. Can you please show some examples?
You could use tonumber to shorten toDec:

toDec = function(num)
  return tonumber(num, 2)
end
You could just use multiplication/division to shorten your shifting functions:

lshift = function(num, amount)
  return num*(10^amount)
end
rshift = function(num, amount)
  return math.floor(num/(10^amount))
end
I dont get how those shifts would work, but Im going to use the tonumber one. Thank you, and Im going to edit the OP, so it states this was more of a practise with understanding binary
Symmetryc #18
Posted 31 May 2013 - 07:46 PM
And I got the same results whatsoever, with the binary and bit api..
That doesn't happen for me…

I dont get how those shifts would work, but Im going to use the tonumber one. Thank you, and Im going to edit the OP, so it states this was more of a practise with understanding binary
Let's say you have a number 1011101 when shifted by 1, it would be 10111010, correct? Well, when multiplied by 10^1, it would also be 10111010.
Engineer #19
Posted 31 May 2013 - 07:55 PM
Well, I have proof it does work, Im supposing you did not use it in the intended way:


And on the second one, it is a string, not a number! :P/>
Symmetryc #20
Posted 31 May 2013 - 08:00 PM
Well, I have proof it does work, Im supposing you did not use it in the intended way:


And on the second one, it is a string, not a number! :P/>
How is that prove anything about the default api when you only printed one thing with it :P/>? Maybe there is some confusion, what I'm trying to say is that bit.band(100, 1001)~=bit.band(toBinary(100), toBinary(1001)), hence only supporting decimal input, whereas your api supports binary input. There is no such thing as something that supports both without a flag argument.
Engineer #21
Posted 31 May 2013 - 08:03 PM
That is because toBinary returns that value in base-2. So that would be only for example, take base-10 5 converted into base-2: 0101.
But since lua cant store bits properly, I chose for it to store them in a string. So you should use the bAnd gate provided with the API.

Can you tell me where I say the toBinary is supported by the default API? :huh:/>
Symmetryc #22
Posted 31 May 2013 - 08:05 PM
That is because toBinary returns that value in base-2. So that would be only for example, take base-10 5 converted into base-2: 0101.
But since lua cant store bits properly, I chose for it to store them in a string. So you should use the bAnd gate provided with the API.
That's what I'm saying :P/>. Lua does not work with base 2 properly, therefore there is a point of your API :P/>.
Engineer #23
Posted 31 May 2013 - 08:09 PM
That is because toBinary returns that value in base-2. So that would be only for example, take base-10 5 converted into base-2: 0101.
But since lua cant store bits properly, I chose for it to store them in a string. So you should use the bAnd gate provided with the API.
That's what I'm saying :P/>. Lua does not work with base 2 properly, therefore there is a point of your API :P/>.
Lol that was quite a discussion :P/>
I guess my English was disturbing the statement you wanted to make. My apologies for that..

:)/>
Symmetryc #24
Posted 31 May 2013 - 08:15 PM
Lol that was quite a discussion :P/>
I guess my English was disturbing the statement you wanted to make. My apologies for that..

:)/>
Nah, I probably posted without reading your post entirely (really, really bad habit of mine :/).

Anyways, I think I'm going to create a Binary API now XD. It's going to use a very interesting concept to make everything extremely, extremely easy for the end user. You'll see :P/>.