MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
Bitwise operators are used to perform operations on binary values. MySQL supports several bitwise operators, including:
Bitwise AND (&
): Returns a binary value where each bit is the result of a logical AND operation on the corresponding bits in the input values.
Example: SELECT 12 & 5;
//Returns 4
Explanation: In binary, 12 is 1100
and 5 is 0101
. The bitwise AND operation gives 0100
, which is 4 in decimal.
Bitwise OR (|
): Returns a binary value where each bit is the result of a logical OR operation on the corresponding bits in the input values.
Example: SELECT 12 | 5;
//Returns 13
Explanation: In binary, 12 is 1100
and 5 is 0101
. The bitwise OR operation gives 1101
, which is 13 in decimal.
Bitwise XOR (^
): Returns a binary value where each bit is the result of a logical XOR operation on the corresponding bits in the input values.
Example: SELECT 12 ^ 5;
//Returns 9
Explanation: In binary, 12 is 1100
and 5 is 0101
. The bitwise XOR operation gives 1001
, which is 9 in decimal.
Bitwise NOT (~
): Returns a binary value where each bit is flipped.
Example: SELECT ~12;
//Returns -13
Explanation: In binary, 12 is 1100
. The bitwise NOT operation gives 0011
, but MySQL uses two's complement for negative numbers, so the result is -13
in decimal.
Left Shift (<<
): Shifts the bits of the first operand to the left by the number of positions specified by the second operand. New bits on the right are filled with zeros.
Example: SELECT 5 << 2;
//Returns 20
Explanation: In binary, 5 is 0101
. Shifting two positions to the left gives 10100
, which is 20 in decimal.
Right Shift (>>
): Shifts the bits of the first operand to the right by the number of positions specified by the second operand. New bits on the left are filled with zeros.
Example: SELECT 5 >> 2;
//Returns 1
Explanation: In binary, 5 is 0101
. Shifting two positions to the right gives 0001
, which is 1 in decimal.
Remember that these operators work on the binary representation of numbers, so their results can be surprising if you're thinking in terms of decimal numbers.
MySQL bitwise operators examples:
SELECT 5 & 3 AS bitwise_and, 5 | 3 AS bitwise_or, 5 ^ 3 AS bitwise_xor, ~5 AS bitwise_not;
How to use bitwise AND operator in MySQL:
SELECT column1 & column2 AS result FROM table_name;
Bitwise OR operator in MySQL queries:
SELECT column1 | column2 AS result FROM table_name;
XOR operator usage in MySQL bitwise operations:
SELECT column1 ^ column2 AS result FROM table_name;
Bitwise NOT operator in MySQL calculations:
SELECT ~column1 AS result FROM table_name;
Applying bitwise operators to integer values in MySQL:
SELECT column1 & 15 AS result FROM table_name;
Using bitwise shifts in MySQL:
SELECT column1 << 2 AS left_shift, column1 >> 1 AS right_shift FROM table_name;
Performing bitwise operations with MySQL expressions:
SELECT (column1 & 3) | (column2 << 2) AS result FROM table_name;
Bitwise operators and binary representation in MySQL:
SELECT BIN(column1 & column2) AS binary_result FROM table_name;
Examples of bitwise operations in MySQL queries:
SELECT (flags & 1) AS is_flag_set, (permissions | 8) AS add_permission FROM user_table;