Storing Binary Combinations in an elegant way - binary

For my problem I have 16 inputs, which can be switched on and off individually and an output that depends on the input combinations. I now want to store the output for each input combination in an one-dimensional-array. This is rather trivial to do if I store every possible input combination, since I can just use the binary coding. The combination 2x0000 0000 0000 1000 would be 10 as a decimal, so I store it as the tenth element of my array.
However, due to storage constraints, I just want to save the combinatons where at least one input is switched on, but also just up to a maximum of n inputs. As an example for a maximum of n=4 inputs, I would want to store
"0010 0100 0101 0000"
but not
"0110 1111 0111 1001"
if that makes sense.
So instead of storing all 2^16=65,536 combinations, I would only need to save 16c4=1,820 (Edit: This number is wrong I think, I can't use nCr here) combinations. But I can't use the binary coding anymore. For example my last entry would be 1111 0000 0000 0000, which is around 61,000 in decimal and not 1,820.
So how could I find this specific combination again?
Currently, I added a second column where I store the binary combination of inputs, but this yet again increases the storage demand. I also now have to search this column for the right combination, increasing computational load as well.
I wonder if there is a sytem to store these combinations and recall them using the same system.

Related

Why is Binary (machine code) based off Boolean Algebra? What would happen if numbers went from 0-9 instead of 0 or 1?

I'm curious to know if it would be possible to create a computer that uses binary that can go from 0000 up to 9999 by having true and false be 1 and 0, but add numbers 2-9 to get more possibilities for numbers. Is binary code only consisting of 0's and 1's for simplicity? Is it because for some reason computers can only understand True and False?
Binary code starts with 0 (0000) and increases to 1 (0001) to 2 (0010) and 10 (1010). Could is be possible for a computer to recognize 0's and 1's but then go to 2's and other numbers? For example, 0000 = 0, 0001 = 1, 0002 = 2, 0009 = 9 then 0010 = 10, and so on.
If this isn't possible somehow, please explain why and give a general explanation of how computers work because I'm interested and want to learn more. If this isn't used because it's inefficient, please epxlain what makes it inefficient and what makes 0's and 1's more efficient.
Thank you.
I expect that it would be possible to create a computer like this but I searched online and couldn't find out why binary code can't have numbers other than 0's and 1's.
Answer to myself for future reference:
Binary is based on Boolean Algebra because it's a base 2 system, and Decimal is a base 10 system that goes from 0-9 instead of 0 or 1 like Binary which is a base 2 system. Computers easily understand binary because its based off on and off states (0 or 1) with 0 being off and 1 being on. Computers use logic gates which are composed of a multitude of transistors that use boolean logic to store data for the computer. Binary makes hardware convenient for computers. Other number systems are used for other purposes different from Binary's purpose. For example, hexadecimal is used to represent numbers that are large in a more simple way that decimal is able to, take the number one million for example,in decimal, it would be 1000000, in binary it would be 11110100001001000000, and in hexadecimal it would be F4240. This is why the Binary number system is based off boolean alegbra and why computers use binary and not other number systems.
It is based on how the data is stored. Each piece of data stored in your memory can have only two values. Think of your memory as a number of glasses which can either be empty or full. Which means the data is stored as bunch of 1s and 0s. This is the result of moving from analog systems to digital, analog values can be between 0 and 1. In analog systems for example, you can have 0.25 or 0.7. But since computers became digital, the logic became binary.
It will be really beneficial to research the history of computers and learn how they evolved over time, if you are interested in this topic.

Binary numbers addition

I have just started doing some binary number exercices to prepare for a class that i will start next month and i got the hang of all the conversion from decimal to binary and viceverca But now with the two letters 'a ' ' b' in this exercise i am not sure how can i apply that knowledge to add the bits with the following exercise
Given two Binary numbers a = (a7a6 ... a0) and b = (b7b6 ... b0).There is a clculator that can add 4-bit binary numbers.How many bits will be used to represent the result of a 4-bit addition? Why?
We would like to use our calculator to calculate a + b. For this we can put as many as eight bits (4 bits of the first and 4 bits of the second number) of our choice in the calculator and continue to use the result bit by bit
How many additions does our calculator have to carry out for the addition of a and b at most? How many bits is the result maximum long?
How many additions does the calculator have to perform at least For the result to be correct for all possible inputs a and b?
The number of bits needed to represent a 4-bit binary addition is 5. This is because there could be a carry-over bit that pushes the result to 5 bits.
For example 1111 + 0010 = 10010.
This can be done the same way as adding decimal numbers. From right to left just add the numbers of the same significance. If the two bits are 1+1, the result is 10 so that place becomes a zero and the 1 carries over to the next pair of bits, just like decimal addition.
With regard to the min/max number of step, these seems more like an algorithm specific question. Look up some different binary addition algorithms, like ripple-carry for instance, and it should give you a better idea of what is meant by the question.

Shifting binary string to a canonical form

I need to compare binary strings but I consider them to be the same if one is a circular shift of the other. For example, I need the following to be the same:
10011, 11100, 01110, 11001
I think it'll be easier to store them in a canonical way, and just check for equivalence. All strings to be compared has the same length (might be more than 100 bits), so I'll keep the length unchanged, and define the canonical form as the smallest binary number we can get from a circular shift. For example, 00111 is the canonical form of the binary strings shown above.
My question is, given a binary string, how can I circular shift it to get the smallest binary number without checking all possible shifts?
If other representation would be better, I'd be happy to receive suggestions.
I'll add, that flipping the string also doesn't matter, so the canonical form of 010011 is 001101 (if as described above) but if flipping is allowed, the canonical form should be 001011 (which I prefer). A possible solution is just to compute the canonization of the string and of the flipped string and choose the smaller.
If it helps, I'm working in MATLAB with binary vectors, but no need for code, an explanation of a way to solve that will be enough, thanks!
Find the longest sequence of 0s, if > 1 shift until the first is the msb.
oops edge case would be 00100, so if you have two equal longest sequences of zeros, you'd want the only possible 1 as the lsb as well.
Find the longest sequence of 1s, if > 1 shift until the last is the lsb
If there are no consecutive 1s or 0s shift until the lsb is 1
That said it's only five shifts so the brute force way you know will work could easily be less effort...

Correct way to store a bit array

I'm working on a project that needs to store something like
101110101010100011010101001
into the database. It's not a file or archive: it's only a bit array, and I think that storing it into a varchar column is waste of space/performance.
I've searched about the BLOB and the VARBINARY type. But both of then allows to insert a value like 54563423523515453453, that's not exactly a bit array.
For sure, if I store a bit array like 10001000 into a BLOB/varbinary/varchar column, it will consume more than a byte, and I want that the minimum space is consumed. In the case of eight bits, it needs to consume only one byte, 16 bits two bytes, and so on.
If it's not possible, then what is the best approach to waste the minimum amount of space in this case?
Important notes: The size of the array is variable, and is not divisible by eight in every situation. Sometimes I will need to store 325 bits, other times 7143 bits....
In one of my previous projects, I converted streams of 1's and 0' to decimal, but they were shorter. I dont know if that would be applicable in your project.
On the other hand, imho, you should clarify what will you need to do with that data once you get it stored. Search? Compare? It might largely depend on the purpose of the database.
Could you gzip it and then store it? Is that applicable?
Binary is a string representation of a number. The string
101110101010100011010101001
represents the number
... + 1*25 + 0*24 + 1*23 + 0*22 + 0*21 + 1*20
As such, it can be stored in a 32-bit integer if were to be converted from a binary string to the number it represents. In Perl, one would use
oct('0b'.$binary)
But you have a variable number of bits. Not a problem! Just process them 8 at a time to create a string of bytes to place in a BLOB or similar.
Ah, but there's a catch. You'll need to add padding to get a number divisible by 8, which means you'll have to use a means of removing that padding. A simple approach if there's a known maximum length is to use a length prefix. e.g. If you know the number of bits is never going to exceed 65,535, encode the number of bits in the first two bytes of the string.
pack('nB*', length($binary), $binary)
which is reverted using
my ($length, $binary) = unpacked('nB*', $packed);
substr($binary, $length) = '';

tricky binary multiplication

I attempted to multiply binary 1111 as first input and 1111 as second input. When I multiply as usual I came across having to do the addition below I encounter having to carry the 1 with the three 1's which would mean 4 in binary with 2 bits. But that's impossible to represent 4 in 2 bits for this multiplication problem.
If you want to add multiple binary values, then you just carry whatever is left over after adding a column, regardless of how many bits you need to represent the carry.
It's just like doing the decimal add 99+99+99+99+99+99+99+99+99+99+99+99, when adding the least significant column, you end up with 108, so you carry 10 eventhough it's too large to fit in a single digit.
Likewise, if you add the binary 11+11+11+11+11 you end up with 101 when adding the least significant column, so you carry 10.
However, normally you only add two binary numbers at a time, as that lets you get away with using a single bit for carry.
What you have to do is carry the numbers over another digit.
Take the scenario:
11
+11
+11
you would have 1001 as your answer because 4 in binary is 100. Simply carry over the 1s into the correct place.