w3resource

MySQL BIT_OR() function

BIT_OR() function

MySQL BIT_OR() function returns the bitwise OR 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 OR operation

After taking two bit patterns of equal length, it performs logical OR operation on each pair of corresponding bits (the first of each; the second of each; and so on).

In each pair, the result is 1 if the first bit is 1 OR the second bit is 1 OR both bits are 1, and otherwise the result is 0.

Syntax

BIT_OR(expr)

Where expr is a given expression.

MySQL Version: 5.6

Example: MySQL BIT_OR() function

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

Sample Output:

mysql> SELECT book_id, BIT_OR('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-or- php mysql examples | w3resource</title>
<meta name="description" content="example-bitwise-or- 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 OR 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_OR("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>"; 
}
?>
</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-bitwise-or</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_OR('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: BIT_AND()
Next: BIT_XOR()



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