All pages
Powered by GitBook
1 of 10

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Bit Functions and Operators

Perform bitwise logic and manipulation. This section covers operators like AND, OR, XOR, and functions for shifting bits or counting set bits in binary data.

BIT_COUNT

Count the set bits. This function returns the number of bits that are set to 1 in the binary representation of a number.

Syntax

BIT_COUNT(N)

Description

Returns the number of bits that are set in the argument N.

Examples

This page is licensed: GPLv2, originally from

SELECT BIT_COUNT(29), BIT_COUNT(b'101010');
+---------------+----------------------+
| BIT_COUNT(29) | BIT_COUNT(b'101010') |
+---------------+----------------------+
|             4 |                    3 |
+---------------+----------------------+
fill_help_tables.sql

<<

Shift bits to the left. This operator shifts the binary representation of a number to the left by a specified number of positions, filling with zeros.

Syntax

value1 << value2

Description

Converts a longlong (BIGINT) number (value1) to binary and shifts value2 units to the left.

Examples

See Also

This page is licensed: GPLv2, originally from

|

Perform bitwise OR. This operator compares bits and returns 1 if at least one of the corresponding bits is 1.

Syntax

Description

Bitwise OR. Converts the values to binary and compares bits. If either of the corresponding bits has a value of 1, the resulting bit is also 1.

>>

Shift bits to the right. This operator shifts the binary representation of a number to the right by a specified number of positions.

Syntax

Description

Converts a longlong () number (value1) to binary and shifts value2

Operator Precedence
fill_help_tables.sql
See also bitwise AND.

Examples

See Also

  • Operator Precedence

This page is licensed: GPLv2, originally from fill_help_tables.sql

|
units to the right.

Examples

See Also

  • Operator Precedence

This page is licensed: GPLv2, originally from fill_help_tables.sql

value1 >> value2
BIGINT
SELECT 1 << 2;
+--------+
| 1 << 2 |
+--------+
|      4 |
+--------+
SELECT 2|1;
+-----+
| 2|1 |
+-----+
|   3 |
+-----+

SELECT 29 | 15;
+---------+
| 29 | 15 |
+---------+
|      31 |
+---------+
SELECT 4 >> 2;
+--------+
| 4 >> 2 |
+--------+
|      1 |
+--------+

~

Invert all bits. This unary operator flips every bit in the operand, changing 1s to 0s and 0s to 1s.

Syntax

~

Description

Bitwise NOT. Converts the value to 4 bytes binary and inverts all bits.

Examples

See Also

This page is licensed: CC BY-SA / Gnu FDL

^

Perform bitwise exclusive OR. This operator returns 1 only if the corresponding bits of the operands are different.

Syntax

^

Description

Bitwise XOR. Converts the values to binary and compares bits. If one (and only one) of the corresponding bits is 1 is the resulting bit also 1.

Examples

See Also

This page is licensed: GPLv2, originally from

Parentheses

Control operator precedence. Parentheses are used to group expressions and enforce the order of evaluation in complex operations.

Parentheses are sometimes called precedence operators - this means that they can be used to change the other operator's precedence in an expression. The expressions that are written between parentheses are computed before the expressions that are written outside. Parentheses must always contain an expression (that is, they cannot be empty), and can be nested.

For example, the following expressions could return different results:

  • NOT a OR b

  • NOT (a OR b)

In the first case, NOT applies to a, so if a is FALSE or b is TRUE, the expression returns TRUE. In the second case, NOT applies to the result of a OR b, so if at least one of a or b is TRUE, the expression is TRUE.

When the precedence of operators is not intuitive, you can use parentheses to make it immediately clear for whoever reads the statement.

The precedence of the NOT operator can also be affected by the HIGH_NOT_PRECEDENCE flag.

Other uses

Parentheses must always be used to enclose .

Parentheses can also be used in a statement between multiple tables to determine which tables must be joined first.

Also, parentheses are used to enclose the list of parameters to be passed to built-in functions, user-defined functions and stored routines. However, when no parameter is passed to a stored procedure, parentheses are optional. For builtin functions and user-defined functions, spaces are not allowed between the function name and the open parenthesis, unless the IGNORE_SPACE is set. For stored routines (and for functions if IGNORE_SPACE is set) spaces are allowed before the open parenthesis, including tab characters and new line characters.

Syntax errors

If there are more open parentheses than closed parentheses, the error usually looks like this:

Note the empty string.

If there are more closed parentheses than open parentheses, the error usually looks like this:

Note the quoted closed parenthesis.

This page is licensed: CC BY-SA / Gnu FDL

TRUE FALSE

Boolean literal for 1 = TRUE or 0 = FALSE, respectively.

Description

The constants TRUE and FALSE evaluate to 1 and 0, respectively. The constant names can be written in any lettercase.

Examples

This page is licensed: GPLv2, originally from

&

Perform bitwise AND. This operator compares bits of two operands and returns 1 only if both corresponding bits are 1.

Syntax

Description

Bitwise AND. Converts the values to binary and compares bits. Only if both the corresponding bits are 1 is the resulting bit also 1.

SELECT 3 & ~1;
+--------+
| 3 & ~1 |
+--------+
|      2 |
+--------+

SELECT 5 & ~1;
+--------+
| 5 & ~1 |
+--------+
|      4 |
+--------+
Operator Precedence
Operator Precedence
fill_help_tables.sql
SQL_MODE
subqueries
JOIN
SQL_MODE
SELECT TRUE, true, FALSE, false;
+------+------+-------+-------+
| TRUE | TRUE | FALSE | FALSE |
+------+------+-------+-------+
|    1 |    1 |     0 |     0 |
+------+------+-------+-------+
fill_help_tables.sql
See also bitwise OR.

Examples

See Also

  • Operator Precedence

This page is licensed: GPLv2, originally from fill_help_tables.sql

&
SELECT 1 ^ 1;
+-------+
| 1 ^ 1 |
+-------+
|     0 |
+-------+

SELECT 1 ^ 0;
+-------+
| 1 ^ 0 |
+-------+
|     1 |
+-------+

SELECT 11 ^ 3;
+--------+
| 11 ^ 3 |
+--------+
|      8 |
+--------+
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near '' a
t line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near ')'
at line 1
SELECT 2&1;
+-----+
| 2&1 |
+-----+
|   0 |
+-----+

SELECT 3&1;
+-----+
| 3&1 |
+-----+
|   1 |
+-----+

SELECT 29 & 15;
+---------+
| 29 & 15 |
+---------+
|      13 |
+---------+