http://tibasicdev.wikidot.com/binairy-data-compression

Hurray for my first contribution anywhere ever.

]]>btw code timings resulted in 0.71 seconds in worst case scenario (taking the 0th bit of a decimal greater than 2^46) (on a 84+)

i edited the routines a bit, they are even somewhat smaller while still working… ]]>

As for the page, you can go ahead and make the page yourself and then put a link to it from the Bin/Hex/Oct page if you wanted. That's a good idea, so I think I'll make a page for my own routines :D

EDIT: Alright, the page should be up here .

]]>assuming E is the number of the bit i want, following this logic: 1_{6}0_{5}0_{4}1_{3}0_{2}0_{1}0_{0} so E=6 results 1…

assuming D is the Decimal number (not zero, not imaginary,not negative, whole)

then the GETBIT function goes as follows

` ``:D→B :0 :For(A,1+int(log(B)/log(2)),0,-1),E //1+int(logB/log2) gives the number of bits there are. this will loop through our Bits, stopping at the E'th bit :0 :If B≥A^2 :Then // if this is true our bit is 1 :B-(2^A→B // subtracts the power of 2 of the bit we are currently at, if it was 1, else does nothing :1 :End:End`

now, the value in Ans contains the bit we wanted

the SETBIT1 function (makes any Bit a 1), follows conventions mentioned above

` ``:prgrmGETBIT //execute previous function :D+(2^E)not(Ans //if Ans(the bit we checked) was zero, we add the appropriate power of 2 to B, making that bit 1, else we leave D alone`

Ans now contains the new Decimal number, with 1 bit of choice set to 1

if the above is clear, next one comes as no surprise

SETBIT0

` ``:prgrmGETBIT :D-Ans(2^E //if Ans(the bit we checked) was 1, we substract the appropriate power of 2 from B, making that bit 0, else we leave D alone`

now, the final routine, changing a 1 to 0 and a 0 to 1, just combines the previous 2

CHANGEBIT

` ``:prgrmGETBIT :D+2^E(1-2Ans`

i optimized this code as much as possible, and it is pretty fast and pretty small for the compression it provides

notice that D can not be bigger than 2^46 because of rounding the calc will do, so a maximum of 46 bits can be stored in 1 Variable.

if this idea is as "clever" as you mentioned maybe it can have its own routine page? it doesn't have that many uses though, but I'd like to give something back to the community after all the info leeching i did last 2 years

]]>i think you just forgot some lines of code there ;)

but it got me thinking, i think i can make it work myself … i'll keep you guys informed when i get to it.

although Burr's suggestion of using lists of 13 digit integers with 1's and 0's is maybe the easiest solution of all…

too bad 'cause i thought it was pretty clever too =D. so i will finish it and afterwards we'll see if it was useful to begin with =)

thanks a lot anyhow =D

]]>Decimal to binary:

` ``:Input "DEC: ",D :"_→Str1 //Just a space :If not(D:"0→Str1 :While D>0 :If not(fPart(D/2:Then :"0"+Str1→Str1 :Else :"1"+Str1→Str1 :End :iPart(D/2→D //Forgot this line and the one below it :End`

After that, Str1 will be your entire binary value, which you can get the 1's and 0's by using expr( and sub(.

Binary to decimal:

` ``:Input "BIN: ",Str1 :Delvar DFor(I,length(Str1),1,-1 :If "1"=sub(Str1,I,1 :D+2^(length(Str1)-I→D :End`

After that, your decimal value will be in the variable D.

Note: Something I've noticed is that Bin→Dec seems to be faster than Dec→Bin, but they are both pretty fast in my opinion. Thess will work up to 9e99 (but not 9.9999999e99 for some reason), however I didn't do any prevention/checking for imaginary, negative, or decimal numbers.

]]>if the routines take up more space than the compression "gains" there is indeed no use.

a list of 46 integers takes 426 bytes of RAM

an integer of 13 digits (which equivalents to 46 bits) uses only 18 bytes (!)

so as long as routines are not incredibly slow and take up less than 400 bytes, i think the compression isn't useless…

but i still have trouble finding the solution…

]]>I was thinking of a way to compactly store this when i thought of the following.

lets say i got 6 binary values: 100010, representing 6 doors that are open(1) or closed(0)

in Decimal, this is 34.

now, i need to open door 4 : 100110

in Decimal, this is 38

what i was looking for was ways to do this without working on the "binary-level", going from 34 to 38 with a function *"set the 4th bit of 34 to 1"*

in the same manner I seek a *"what is the 4th bit of 34"* function and a *"change the 4th bit of 34*" function (1 becomes 0 and 0 becomes 1)

anyone has any ideas towards a solution?