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

[ASK] Binary how it works

Started by Hayden_Almeida, 16 November 2015 - 06:35 PM
Hayden_Almeida #1
Posted 16 November 2015 - 07:35 PM
Hello guys, i am working in another game and i need to know how rs.getbundledinput works. What are the steps it does.

Let me say i have 3 itens:

1. Flashlight
2. Radio
3. Key

and i assign the values to this itens (for potential or binary)

1 - Flashlight
2 - Radio
4 - Key

If i have the 3 itens, the value will result in

7
Because 1+2+4 is equal to 7, right?

How can i create a formula to read what item is missing?
Like:
if a have the value 3, what item is missing (or what value in binary is missing) ?
Edited on 16 November 2015 - 06:35 PM
Creator #2
Posted 16 November 2015 - 07:43 PM
You can't say what item is missing, but you can use if:
if result == 5 then
return {1,4}
elseif and so on…
InDieTasten #3
Posted 16 November 2015 - 07:52 PM
Theory: (mathematical)

Devide 3 by the upper most item value:

3 / 4 = 0.75
now floor that: 0 <- which means, you do not have a Key

you need to get the rest of the integral division though, in case you would've had the key:
3 % 4 = 3
now you repeat that down until you are left with either 1 or 0, determining, whether you've got a flashlight or not

Snippet: (mathematical)

function items(value)
	local Key = math.floor(value/4)
	local rest = value % 4
	local Radio = math.floor(rest/2)
	local Flashlight = rest % 2
	return Key, Radio, Flashlight
end

items(0) --# returns 0,0,0
items(1) --# returns 0,0,1
items(2) --# returns 0,1,0
items(3) --# returns 0,1,1
items(4) --# returns 1,0,0
items(5) --# returns 1,0,1
items(6) --# returns 1,1,0
items(7) --# returns 1,1,1



Theory: (digital)
When you can use bitwise operations, you might as well determine the existance of a base2 digit by applying a so called 'mask'
Your number in bits: 0b011 being a decimal 3
0b111 would be 7


now with bitwise operations, you can do something like the following
0b001 – mask for Flashlight
AND
0b011 – number to check
=
0b001 – and when this result is the same as the mask, then you have that item


0b010 – shifting the 1 to mask for the Radio
AND
0b011 – number to check
=
0b010 – equals the mask, so you got a radio


0b100 – mask for Key
AND
0b011
=
0b000 – does not equal the mask, therefor no Key for you today

This bitwise approach is actually doing exactly the same as the mathematical approach, but when you are able to get the work out into a library(like a bit32 library), you can concentrate more on writing your actual thing, than worrying about the right exponent for each and everything. So the approach using a bit library is much more flexible and extendable


Further
Now the only thing remaining is, do you actually need to do this? Because you might as well just store them as number(allowing you to have multiples of each item) or as bool, to not interchange the formats all the time ;)/>
Edited on 16 November 2015 - 07:06 PM
InDieTasten #4
Posted 16 November 2015 - 08:09 PM
You can't say what item is missing, but you can use if:
if result == 5 then
return {1,4}
elseif and so on…
This can do the job, although it's a really crappy solution. When he is talking about 10 different items, instead of 3, you will clearly run into a problem with this.
Not to say theres no solution at all. I already posted 2 possible solutions, breaking the number down into its powers of 2s
Creator #5
Posted 16 November 2015 - 08:25 PM
You can't say what item is missing, but you can use if:
if result == 5 then
return {1,4}
elseif and so on…
This can do the job, although it's a really crappy solution. When he is talking about 10 different items, instead of 3, you will clearly run into a problem with this.
Not to say theres no solution at all. I already posted 2 possible solutions, breaking the number down into its powers of 2s

You're right and thanks! :P/> (crappy solution)
MKlegoman357 #6
Posted 16 November 2015 - 09:03 PM
If we have a total of four items we can encode them into four bits: 0101. Here 0 means the item is missing and 1 means it's not. Now we can see we have the first and third items, but not the second and fourth. To check programmaticaly if we have the first item we can use the bitwise AND operator: 0101 AND 0001 => 0001. If we get an answer that is not equal to the number 0 then it means that number is inside. Look at these examples to understand it better:


0101 AND 0001 => 0001 #(answer is not zero, we have item #1)
0101 AND 0010 => 0000 #(answer is zero, we don't have item #2)
0101 AND 0100 => 0100 #(answer is not zero, we have item #3)
0101 AND 1000 => 0000 #(answer is zero, we don't have item #4)

Now a function in CC Lua would look like this:


local function is_inside (items, item)
  return bit.band(items, 2^(item - 1)) ~= 0
end

To use it we'd give it the number of all the items and which item we want to check:


local items = 5 --# 0101

print(is_inside(items, 1)) --# true (the first item is inside the number)
print(is_inside(items, 2)) --# false (the second item is not inside the number)
Edited on 16 November 2015 - 08:05 PM