I. Scenario: In some scenarios, a series of bit types need to be saved. To simplify the database structure or other special requirements, you can save eight bit types as one byte (for example, tinyint type ). Ii. Logic bit operation letter
I. Scenario: In some scenarios, a series of bit types need to be saved. To simplify the database structure or other special requirements, you can save eight bit types as one byte (for example, tinyint type ). Ii. Logic bit operation letter
I. scenarios:
In some scenarios, you need to save a series of bit types. To simplify the database structure or other special requirements, you can save eight bit types as one byte (for example, tinyint type ).
Ii. Logical bit operation functions:
(1) "logical bit and ". When and only when the values of the two bits in the input expression (the current bit being parsed) are both 1, the bits in the result are set to 1; otherwise, the bit in the result is set to 0.
Example: select 170 & 75
1010 1010 <-- 170
0100 1011 <-- 75
-------------------
0000 1010 <-- 10
(2) "logical bit or ". If one or both bits in the input expression are 1 (for the current bit being parsed), the bit in the result is set to 1; if neither of the two bits in the input expression is 1, the bits in the result will be set to 0.
Example: select 170 | 75
1010 1010 <-- 170
0100 1011 <-- 75
-------------------
1110 1011 <-- 235
(3) "logical bit exclusive or ". If the values of the corresponding two bits are both 0 or 1, the value of this bits in the result is cleared to 0; otherwise (only one of the two corresponding bits is 1), and the value of this bits in the result is set to 1.
Example: select 170 ^ 75
1010 1010 <-- 170
0100 1011 <-- 75
-------------------
1110 0001 <-- 225
3. Read and judge:
For example, if (170 & 8) = 8 is used to determine whether the right number of 4th bits is 1. That is, you only need to perform the logical bit and operation on this bit to shield the other seven digits.
1010 1010 <-- 170
0000 1000 <-- 8
-------------------
0000 1000 <-- 8
Custom functions:
-- @ CombinedValue Original Value (10 hexadecimal), for example: 170
-- @ BitPosition: number of digits, for example, 4
-- @ BitValue value, which can only be 0 or 1
-- Return value (decimal), for example: 170
Tip:
SQL Server 2008 provides a value assignment function similar to the C language, such as "& =" (bit and equal), "| =" (at equal), "^ =" (bitwise OR equal ).
This article is from the "MSSQL we have Chased Together" blog. Please keep this source