BInary numbers
-
-
but I need the result as a binary number that I can perform bitwise operations on.
There is nothing like a "binary number", just "binary representation of a number". You can perform bitwise operations on ordinary numbers:
125 & 15 == 125 % 16
If your human brain needs to view the numbers as binary, use the method @ustk to convert it to a string. You might need the reverse function to parse a binary representation string to a number, that should be a fun little exercise.
-
@christoph-hart Yep this works nicely
const mask = 1 << 3; Console.print(((12 & 10) & mask) != false); // 1
-
As a little test here's a snippet that uses a bit mask to figure out which combination of 4 buttons are turned on.
I'll probably make an explanation video about this at some point but if you want a simple guide to this stuff I recommend this.
HiseSnippet 1115.3ocsV81aaTCF2WZuwti+HlDuFYtWftPJYIskAh0pUZZKJh0t.oTgzzzjycNMV0w9jsutEAUhOJ7sguF7AfWv2.3w9tjbsMqzFFQIW7y++4e12icOkLgp0RExK33IYTj2642ehvLpyHBSf5tmUwPF2PUGS0FztSxHZMME44sx2Z8vKXUj6ye8jcIbhHgNWEBchjkPeJaLyLWauc9NFme.IkdLabEu2bmtIRQGIWlCnYE+VnLRxYjSoGQrtUyG4cu8SYFopugXnZvmckoS5OR9JQg+mvzrAbpUnMpOjnB0nNiX7zdSmoZDxa0dym2qTLu+H+CYorY5mO++PmA77HpxAd0tIH09N.IuJPZ0BH8.+9IJVlYtEKddW+tBX4XHAn5pPovWTseeE+NRvCgo4XxYzCTfvrHheTqVqggG0ebXnhdJVjOFFADu1fGjaLv.713m+BP4PoBGy.oVOFyvag2D9qQi5g+bXPomMyx0ihmVsSolNxwYRAHDGsqykHbCaNZfaWuNTxoA9b1KZpstKLJIuCgyG.q0wRQg8R8P.WDFxDblfhGlKRLLo.eEmhSlVy0vmS34TG.gokC3gAgAWcdLE7bp3TynxIUfKjFaiyHJME3q3CIlQMyjuJd80vr53OCWA6vT8DashqWfwG9P7.i36yopIS4xRQnrKlel5PDjgoieCbRo04rxh3jK6zhYE1PbboT.HFvkIDN1tf63KfgvOAGcjb1NAhhhM4JAMEJQyH7Wiih.NEH0aKqZKiqtw1R7o3313s1B3y53OYa7PBWawRoaN+zVCQQNsAAZ6RRDlHRwPgCmq5ZaufGQXldNbc.M3hvhevWfZzRNsYlhAK.Z61wKBunBGdjzPelH1QUvpJ9plFNbg1JobNUsPy1tbpaJPKyLfpptPYcDZCb4dK2610aIoX6VEGkhtBl4YYzR4Cj7TaOC63q2IBUte01lrDgfqFWGoOnriTA6iXPRB7KDZibvu5ABn6P3qe0vKAxsL7M9uE9lKH7er6dDCw1YtjP.RJipLL6Zg2dzygi1J5SG3uGUelQlg77m8FNL9VvYut54eSlI7qjcnBBr3ZO54oThRX6vitSIe8JI+OReKm7Mpj7eS+VN4aVE4+xcJ42+Ml7P+osHsY226Og7MwN3i8PHC80Pvuieg8ETCl9P4XnBDvrmWMM4b3LhdJJzv1Agqe3MbsAYZNmXt7cIrWZpz.zD3RGfaOjVnYlIUeG5NbAiV23ELtsP7A98XljQKFi0V.Fg2P9+.ikWK6882e3PZhYN.W0+feZYuC1+R4+AYtgINEN4WwraRNJebe31nITn5BAkqs6WpYasTH2xJaYf9TQpS3ugOkFaak8JM1dpQzXRhR9xjhdS1K9cemF.SB28bC7OzJim0O02uUyVnwvtwWljXm9eNz0dwwr9RDyFKQLatDw7EKQLOZIh4KWhX9paLFaifuI2HGW75.nn29tiF77120ovsKD8Oz0KehB
-
@d-healey It is indeed interesting programmatically, but isn't that a bit convoluted for such a simple task?
Or do you intend to use it something else, maybe? -
@ustk said in BInary numbers:
but isn't that a bit convoluted for such a simple task?
Do you have a method that uses less lines of code to display every possible combination of x number of active buttons?
@ustk said in BInary numbers:
do you intend to use it something else,
I'm using to filter a list. Each button is a different attribute that can be filtered.
-
@d-healey Well, as for just displaying, yes it can be much simpler but I know you already know what I will show you below, so if you do it your way, I'm sure there is a good reason behind it, like for filtering your list
HiseSnippet 1065.3ocsV0uaaaCDmxNZqx6CzBrG.B+OSFNy0Nwsa.YCKKNICFqI0aNKX.EEEzRzwDghTijJqFaAnOJ6QqOB6MX6n9vlI0oHwK0vvV22+zc7tiiTxHpVKUHuf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VBvM7O729vbQFzuHyLv5qiHFECNr3ebVxX35cQTH5BAkaG.6UyNhoftqk1lAFSEw4D+K7oTXOKsWovdUBQIjHk7UQEynr2d5A4b.LIxu3Xf+QVZ7h4p99c6zEk.mFeUTj80+qfo2q1lsVCa1dMro+ZXySVCad5ZXyWuF17MuWarCB9gLiLonc.XL5f7UDddGjOoH+TH5+.OiUWF
-
@ustk said in BInary numbers:
I'm sure there is a good reason behind it, like for filtering your list
I'm not always so sure, sometimes I see a path and follow it, but it's not always the right one :)
In this case I want to use a switch statement. So if button 1 is on, do something, and if button 2 is on do that as well, etc. I figure I can run a switch on a single value more easily than multiple if statements over an array, but I might be wrong.
-
@d-healey So if I understand well, you need a single number representation of the state of all buttons at a time?
In this case, effectively this is the best solution I can see... -
@d-healey You can also switch over an array, it has the advantage of giving easy readable code, but it might not be compatible with what you want to achieve...
inline function onbtnQueryControl(component, value) { if (value) { local state = []; for (i = 0; i < buttons.length; i++) state.push(buttons[i].getValue()); Console.clear(); switch (state) { case [0,0,0,1]: Console.print("Do this"); break; case [0,0,1,0]: Console.print("Do that"); break; case [1,0,1,1]: Console.print("Do whatever"); break; default: Console.print("Do nothing"); } } }
-
@ustk Oh I didn't know that! For 4 buttons I think that's more readable, when you get up to 10+ buttons I think it might become unpleasant.
-
@ustk that works? I didn‘t knew you can use array literals as switch cases.
But if this comes in the vicinity of a realtime callback I wouldn‘t trust it not allocating so a bit fiddling might be better suited.
-
@d-healey Note that it can also be replaced by the string representation of the same binary number.
I'm not sure to agree with the fact that it can become unpleasant, because it's always easier to debug or maintain a visual representation rather than a state you don't "see", but it's a matter of taste of course... -
@christoph-hart Yep that works! I understand the allocation issue though... This is where using a string would be better
-
inline function onbtnQueryControl(component, value) { if (value) { local state = ""; for (i = 0; i < buttons.length; i++) state += Math.round(buttons[i].getValue()); Console.clear(); switch (state) { case "0001": Console.print("Do this"); break; case "0010": Console.print("Do that"); break; case "1011": Console.print("Do whatever"); break; default: Console.print("Do nothing"); } } }
-
@ustk said in BInary numbers:
a state you don't "see"
-
-
@ustk You were right, I found a cleaner way to do it without resorting to bit masks :)
-
@ustk nope, that's even worse, you're creating a string in the button callback and it might reallocate it in each loop iteration :)
-
@christoph-hart then using a reg should resolve the issue right? For a better world? :)