When Is A List An Integer?
Posted by Charlie Recksieck on 2019-07-11
A Cool Trick, Should You Need It
If you're reading this with no awareness or need to write code, apologies to you. I'm not going to attempt to define all programming terms here, but this still might be of some interest to you.
Basically, the idea is that sometimes one item (in code, in a database) needs to have multiple characteristics. Normally, a list is the way to do this. That will suffice in 99.9% of all coding situations, a list is a valid way to supply arguments to a function and in databases you can query a value to another table to lookup multiple records. Nonetheless, take a look at this.
Let's set up a list of things that a person could get at the store:
0 = Nothing
1 = Milk
2 = Bread
4 = Cheese
8 = Potato Chips
16 = Ice Cream
(sorry for the unhealthy list)
Code would usually set up some list like new List
But what if we just used an integer like this: new intDairy = 21?
You basically add up all of the numbers in the list, if they are indexed like they are above (0, 1, 2, 4, 8, 16 or n = 2^a where a = 1 to 5).
Consequently, if we got an argument of 21, the only combinations it could be would be adding together Milk (1), Cheese (4) and Ice Cream (16). Go ahead, play around with the math.
Pretty neat, huh? When would you actually need to use this? I'm not totally sure.
You could use this in a database. One table would have this:
Name | FoodPurchased
Dairy Ice Cream
... so some query to this table could return a recordset of multiple food items. Again, basic database stuff. If you can't work with relational tables or deal with some sort of a Join query, you probably should get out of the database business.
Yet as hokey of a method as this would seem in a database, this theoretically would allow the database to occupy less space. A tiny 4 bytes or storage required for the integer 21 instead of whatever memory storage the other whole table occupies.
But I'll give you a real-world example of this method in Autocad. When drawing or placing objects in CAD drawings a "snap" is a prompt where the program helps you place something. If I want to place a circle at the endpoint of a line, then if I have the END (endpoint) snap turned on, then as long as I move the proposed new circle near the end of the line, when I mouse click, it will automatically "snap" the circle to the end of the line.
There are several types of Autocad OSnaps defined here in a similar table with the same 2^a index as our food table above:
0 = NONe
1 = ENDpoint
2 = MIDpoint
4 = CENter
8 = NODe
16 = QUAdrant
32 = INTersection
64 = INSertion
128 = PERpendicular
256 = TANgent
512 = NEArest
1024 = QUIck
2048 = APParent Intersection
4096 = EXTension
8192 = PARallel
When inserting or moving an object in Autocad, there are available buttons to change your OSNAP settings in the middle of code, so that's easy enough as a human user. But if I want to code a command to insert or prompt to insert an Autocad entity, then I have to use a command line to execute it, with the trick being that the Autocad command line does not accept a list of parameters for a lot of settings.
Instead, we have to pass an integer to the OSMODE Autocad variable/setting. So if we set OSMODE in Autocad with the code as "785".
That simple little 785 integer value tells Autocad that that new OSnaps we want defaulted are Endpoint (1), Quadrant (16), Tangent (256) and Nearest (512). There are no other possible combinations that could add up to 785. Voila! Read more on this Autocad OSMODE trick here
Sock this idea away somewhere if you ever need it or want to play around with it. And if you do, PLEASE let me know about it, would love to see another person's practical use of this.
Til then, I just think this is intriguing.