Responsive Ads Here

Tuesday, April 14, 2020

Bitwise operators | Bitwise operators details explanation

The Bitwise Operators

The bitwise operators allow you to manipulate individual bits. Bitwise operators perform Boolean algebra on the corresponding bits on the two argument to produce the result.

Bitwise Operator in C 

The bitwise operators come from C's low-level orientation, where you often manipulate hardware directly and must set the bits in hardware registers.

Bitwise Operator in Java

Java was originally designed to be embedded in TV set-top boxes, so this low-level orientation still made sense. However, you probably won't use the bitwise operators much.

Let's start with bitwise operators feature in details.

1. Bitwise AND operator (&) 

The bitwise AND operator (&) produces a one(1) in the output bit if both input bits are one (1). otherwise, it produces a zero (0). 

2. Bitwise OR operator ( | ) 

The bitwise OR operator ( | ) produces a one (1) in the output bit if either input bit is a one (1) and produces zero (0) only it both input bits are zero (0).

3. Bitwise EXCLUSIVE OR, or XOR ( ^ ) 

The bitwise EXCLUSIVE OR, produces a one in the output bit if one or the other input bit is a one (1), but not both.

4. Bitwise NOT ( ~ ) 

The bitwise NOT is a unary operator; It takes only one argument. All other bitwise operators are binary operators. Bitwise NOT operator is also called the ones complement operator. Bitwise NOT produces the opposite of the input bit - A one (1) if the input bit is zero (0), a zero (0) if the input bit is one.


Bitwise operators
Bitwise Operators











The bitwise operators and logical operators use the same characters. The bitwise operators can be combined with the = sign to unite the operation and assignment : &=, |= and ^= are all legitimate. Since (~) is a unary operator, it cannot be combined with the = sign.

The boolean type is treated as a one-bit value, so it is somewhat different. You can perform a bitwise AND, OR and XOR, but you can't perform a bitwise NOT. 
For boolean, the bitwise operators have the same effect as the logical operators except that they do not short circuit. 


Bitwise operator in java with example

Let write some code in java to implement bitwise operators.

package com.javawins;

public class Bitwise {

 public static void main(String[] args) {
  byte first = 5;
  byte second = 4;
  System.out.println("First Binary Number : " + Integer.toBinaryString(first));
  System.out.println("Second Binary Number : " + Integer.toBinaryString(second));
  int bitwiseAnd = first & second;
  System.out.println("1. Bitwise AND for above numbers  : " + Integer.toBinaryString(bitwiseAnd));
  int bitwiseOR = first | second;
  System.out.println("2. Bitwise OR for above numbers  : " + Integer.toBinaryString(bitwiseOR));
  int bitwiseXor = first ^ second;
  System.out.println("3. Bitwise XOR for above numbers  : " + Integer.toBinaryString(bitwiseXor));
  int bitwiseNOT = ~first;
  System.out.println("4. Bitwise NOT for first numbers  : " + Integer.toBinaryString(bitwiseNOT));
 }

}



Output

First Binary Number : 101
Second Binary Number : 100
1. Bitwise AND for above numbers  : 100
2. Bitwise OR for above numbers  : 101
3. Bitwise XOR for above numbers  : 1
4. Bitwise NOT for first numbers  : 11111111111111111111111111111010

From the give output it's has been very clear bitwise AND , OR. In XOR Preceding 1 is 0 that has not be printed.
In Bitwise NOT for 101 is 010 that is the last three bit of fourth output, and because in java 32 bit is reserve for int all the preceding 29 bit zero converted to 1 and printed in output.

Next Ternary operator









*******

ArrayList best explanations for interview point of view


Ocjp latest dumps | Ocjp certification dumps


How to sort arraylist in java 




No comments:

Post a Comment