Transcript Document

Welcome to
CODE SPREAD
Simple Concepts of Coding | Programming
What is Bitwise operator?
• The bitwise operators are a Transact-SQL
extension for use with the data-type integer.
• These operators convert each integer operand
into its binary representation and then evaluate
the operands column by column.
• A value of 1 corresponds to true; a value of 0
corresponds to false.
Scenario :
We have one webpage called “Product Listing”
which displays data from table “Product”, which
contains data related to ‘n’ number of products.
Also, this webpage should have a filter column
which follows some rule like-
• On click of any filter criteria, products satisfying
the filter criteria should be displayed.
Scenario : (contd.)
Scenario : (contd.)
To implement logic of Bitwise operator. We have
a column BitValue in our FilterItem Table.
Scenario : (contd.)
This column BitValue in our FilterItem Table has
values like- 1,2,4,8,16,32. Just look at the binary
representation of these values
1 | 00001
2 | 00010
8 | 00100
16 | 01000
32 | 10000
Scenario : (contd.)
These values are obtained by shifting 1 to the
right in the binary representation. Now, we have
to find out, which of our product belongs to
which filter.
Scenario : (contd.)
To achieve that, we have BitValue column in our
Product Table.
Scenario : (contd.)
We will perform ‘AND’ operation between ‘BitValue of
Product table’ and ‘BitValue of FilterItem table’.
For Product1, we have BitValue 5. On performing AND
operation between the binary representation of 5 [0011]
and FilterItem’s Bitvalue values, we find out that Item1
having BitValue [0001] and Item3 having BitVaue [0010]
gave result as ‘0’.
That means, ‘product1′ belongs to ‘Item1′ and ‘Item2′ of
FilterItem table.
Scenario : (contd.)
On Click of any of these items, product 1 will definitely
get displayed. Same ‘AND’ operation can be performed
for other products and their filters can be determined.
Few Points :
• Save multiple values into one column instead of creating multiple
columns.
• Since we will be storing the Bit Mask Values in our database like
1,2,4,8,16,32,64,…,2^n (its left shift Masking example
0001,0010,0100,1000 etc. We are shifting the Bit 1 left side every
time for next values).If we take datatype as BigInt for our column
then it can have max value up to 9223372036854775807. So
approximately up to 50 values (2^50) we can store.
• “AND” operator works in C#. net similar to SQL server, So we are
free to use it at front end or back end as per our convenience.
• By using this logic, our database queries will be faster since we will
be operating on integer data type (Which is faster than Bit data type
field and IN operator)
• Also, while fetching the records we are avoiding full table scan.
Thanks!!
CODE SPREAD
Simple Concepts of Coding | Programming