w3resource

MySQL BIT_AND() function

BIT_AND() function

MySQL BIT_AND() function returns the bitwise AND 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 18446744073709551615.

What is Bitwise AND operation

A logical AND operation is performed on each pair of corresponding bits of two binary expressions of equal length.

In each pair, it returns 1 if the first bit is 1 AND the second bit is 1. Else, it returns 0.

Syntax:

BIT_AND(expr)

Where expr is a given expression.

MySQL Version : 5.6

Example: MySQL BIT_AND() function

The following MySQL statement performs Bitwise AND 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_AND('book_price') AS BITS
FROM book_mast group by book_id;

Sample Output:

mysql> SELECT book_id, BIT_AND('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.04 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-of-bitwise-and- php mysql examples | w3resource</title>
<meta name="description" content="example-of-bitwise-and- 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 AND 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_AND("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>

View the example in browser

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-of-bitwise-and</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_AND('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>BITS</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>

Previous: AVG()
Next: BIT_OR()



Inviting useful, relevant, well-written and unique guest posts