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

Some questions about bit manipulation.

Started by tonyduan, 28 June 2013 - 10:24 AM
tonyduan #1
Posted 28 June 2013 - 12:24 PM
I use MineFactory's bundled wire to control something.I know the method to set the output of the wire.The method is "redstone.setBundledOutput(String side,int color)". color is a integer converted from binary number.For example:

colors.white = 1 (decimal) = 0000000000000001(binary)
colors.orange = 2 (decimal) = 0000000000000010(binary)

So, if i need to turn both white and orange on ,i just input 1+2 to color.When i need to turn orange off,i let color minus 2.But,if i don't know the value of color first,a error may occur.For example,color is 1,and after minus it becomes -1.
Thus,I think i need to do some bit manipulation.
Example:

value = 0100100000000010
if i want to turn off orange,i change value into
0100100000000000
so,my question is how to convert a decimal number into binary number?
And how i can insure a specific digit is 0 or 1 ?
Lyqyd #2
Posted 29 June 2013 - 12:05 AM
Split into new topic.

All of the bit manipulation is already done for you in a convenient API. You simply get the current output of the side you want to change (rs.getBundledOutput(side)), then use colors.subtract and colors.combine to manipulate the returned value, and set the output again. It ensures that the output is correct, even if you try to turn off a color that's already off. This is going to be a much simpler process if you use the colors API and don't try to manipulate the values directly.
tonyduan #3
Posted 29 June 2013 - 03:32 AM
Split into new topic.

All of the bit manipulation is already done for you in a convenient API. You simply get the current output of the side you want to change (rs.getBundledOutput(side)), then use colors.subtract and colors.combine to manipulate the returned value, and set the output again. It ensures that the output is correct, even if you try to turn off a color that's already off. This is going to be a much simpler process if you use the colors API and don't try to manipulate the values directly.
sorry,i didn't notice that methods.And,thanks for help! :D/>