SQL Server logical bit operation example

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.