I am trying to set up a compression system for Midnight that should be really simple to do. The problem is I'm missing the math somewhere.
Here's what I would like to know how to do: Say I have the number 834 (512+256+64+2). How do I check to see if various powers of two are represented by that number? I.E., I want a mathematical equation that tells me if 2^(any number) is in that number. I hope that explained it well enough. To clarify, 16 is not a number in 834, but 256 is. Likewise, 16 is a number in 1155 (1024+128+2+1), but 2 is. How can I figure this out with a mathematical equation so I don't have to resort to loops? Thanks in advance for any help I receive!
I think the fastest way would be to use loops unless you can use bit-logic
Is this BASIC?
Grammer Download (2.29.04.12) Latest update (possibly incomplete) My pastebin Spoiler for Graphiti:
This is a graph explorer for graph theory. It will require lots of work to finish. Currently you can:
Add edges (direction not shown, but they
Arrange vertices in a circle (in the future, you will be able to define levels of rings and the number of nodes in each)
Create complete graphs quickly
Add adjacency matrix viewer
Multiple graphs support
Arrows for directed graphs
Here's what I came up with:
iPart(fPart(A/2^B/2)*2) A is the number to examine, and the formula checks if 2^B is a binary component of A. B=0 would check if 1 is a component, B=1 would check if 2 is a component, B=2 would check if 4 is a component, etc.
To extract bit N of a value, do floor(value/2^N)%2
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman
It's actually a lot simpler than it may sound. It's just a bit trickier in TI-BASIC because TI-BASIC has no binary data types. To use your number as an example, if you break down 854 into binary, you get:
512 64 4 | | | 00000011 01010110 | | | 256 16 2 This says that 854 = 512+256+64+16+4+2. And there's your answer, right in there. Each bit tells you whether 2^([bit position from right]) is a binary component of the number. The trick is isolating the bit that you care about. I do this by rotating the number to points where TI-BASIC is able to mask off the bits I don't want. The functions I have to work with are iPart() and fPart(), which mask on either side of the decimal point, so I have to shift the bit I want immediately next to the decimal point to be able to use these effectively. First I do this by dividing by 2^([bit position]+1), which effectively shifts the binary number right [bit position]+1 places: 00000000 00000001.10101011 0 | 256 Then I can mask off all the bits on the left with fPart() (in this case, just the bit representing 512): 00000000 00000000.10101011 0 | 256 I then multiply by 2 to shift the number back one place to the left: 00000000 00000001.01010110 | 256 And finally, I can use iPart() to mask off all the bits on the right: 00000000 00000001.00000000 | 256 So I'm left with the bit representing 256 in the one's place of my number and all other digits gone, thus giving me the boolean value 1, which tells me that 256 is a binary component of 854.
Last Edit: 14 November, 2011, 03:26:58 by Runer112 »