Bit-masking, Bitwise operation, what is it? How do I use it in daily basis?
Hello mate, in this short article I want to share about bit-masking and bitwise operation based on my own experience. Some of you might knowing this technique very well and more experience than me, hopes you guys enjoying this article.
What is Bitwise operation?
Bit wise operation is logical operation that evaluate bit by bit and returned as binary digits too.
By that definition above, we could not run logical operation over a number type of variables like unsigned integer or signed integer at least on statically type programming languages like Go and C#. JavaScript treat numbers using logical operator is having the same result by using bitwise operation.
Bitwise operators
Shift operation is moving the bits by a given number to move. In example 3 << 1 here is the explanation:
3 is 0b00000011 and shifted to the left by 1 position resulting to 6 or 0b00000110 in bits representation.
By those explanations above, this operation could replace a procedure that including arrays of Boolean which allocates more than a single integer variable.
What is Bit-masking and how does it corelate with Bitwise operation?
bit masking is a process of operation to achieve a conditional state utilizing binary representation. Like we discussed it above that we could replace a procedure that including arrays of Boolean. Let we have an array of Boolean [false, false, true] and we want to check what is the value of the index 1 of that array. Using that implementation the solution is straight forward, just access that array using index of 1 so we have false value. Now Let us change that array of Boolean with binary representation thus we have 0b00000100 or a 4 as 8 bits unsigned integer. How do we access the condition of the index 1 in this representation? Here is the procedure:
The visual representation of array is left to right while in bits representation is from right to left
领英推荐
By this definition, we can obtain by:
How about the index of 3? 0b00000100 & 0b00000100 = 0b00000100 or 4 means that the value is true.
The operation above is one of the bit-masking technique.
My Experience with Bit-masking
My experience using this technique is used in multiple adapter initiation and unit test assertion. Unit test is not required to be optimized that much, but I keep use it in several cases to keep me motivated from boring CRUD tasks.
Multiple adapter initiation
Let we have a multiple adapter of a connection string to a database. We define the index of the connection string either in configuration or hardcoded in source code (if your multiple connection string is most likely not updated in very long time). We provide a function that return a connection string with given an activation schema and returning the array of string which is the connection string. The activation schema is binary representation of array of Boolean which database is activated for some application. In this function we evaluate the activation schema with bit-masking as we discussed above. If the result is zero, we do not fill the connection string and remains empty string, vice versa.
After that, we pass this connection strings to the database initiation function that return array of database adapters.
Closing
Thats all, I am happy that I am writing this article in order to share with you, and more important is as a note to myself also. Thank you, adios.