MySQL BIT_XOR() function
BIT_XOR() function
MySQL BIT_XOR() function returns the bitwise XOR of all bits in a given expression.
The calculation is performed on 64 bit precession.
If this function does not find a matching row, it returns 0.
What is Bitwise XOR operation
After taking two bit patterns of equal length, it performs logical XOR operation on each pair of corresponding bits (the first of each; the second of each; and so on).
The result in each position is 1 if the two bits are different, and 0 if they are same.
Syntax:
BIT_XOR(expr)
Where expr is a given expression.
MySQL Version: 5.6
Example: MySQL BIT_XOR() function
The following MySQL statement performs Bitwise XOR operation on the values of book_price column. A grouping on book_id column is also performed.
Sample table: book_mast
Code:
SELECT book_id, BIT_XOR('book_price')
AS BITS from book_mast group by book_id;
Sample Output:
mysql> SELECT book_id, BIT_XOR('book_price') AS BITS from book_mast group by book_id; +---------+------+ | book_id | BITS | +---------+------+ | BK001 | 0 | | BK002 | 0 | | BK003 | 0 | | BK004 | 0 | | BK005 | 0 | | BK006 | 0 | | BK007 | 0 | | BK008 | 0 | | BK009 | 0 | | BK010 | 0 | | BK011 | 0 | | BK012 | 0 | | BK013 | 0 | | BK014 | 0 | | BK015 | 0 | | BK016 | 0 | +---------+------+ 16 rows in set, 16 warnings (0.00 sec)
PHP script
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-bitwise-xor- php mysql examples | w3resource</title>
<meta name="description" content="example-bitwise-xor- php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Example of bitwise XOR operation:</h2>
<table class='table table-bordered'>
<tr>
<th>Book ID</th><th>BIT</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT book_id, BIT_XOR("book_price") AS BITS from book_mast group by book_id') as $row) {
echo "<tr>";
echo "<td>" . $row['book_id'] . "</td>";
echo "<td>" . $row['BITS'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>
JSP script
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-bitwise-xor</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data = "SELECT book_id, BIT_XOR('book_price') AS BITS from book_mast group by book_id";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>BOOK_ID</td>
<td>BIT</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("book_id")%></TD>
<TD><%=rs.getString("BITS")%></TD>
</TR>
<% } %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>
Online Practice Editor:
Previous:
BIT_OR()
Next:
MySQL Aggregate Functions and Grouping - COUNT()
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework