Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. package bitwiseapp;
  2.  
  3.  
  4. public class BitwiseOperations {
  5.     private int a;
  6.     private int b;
  7.  
  8.     public int getA() {
  9.         return a;
  10.     }
  11.  
  12.     public void setA(int a) {
  13.         this.a = a;
  14.     }
  15.  
  16.     public int getB() {
  17.         return b;
  18.     }
  19.  
  20.     public void setB(int b) {
  21.         this.b = b;
  22.     }
  23.    
  24.     public void showlntA() {
  25.         System.out.println("A AS NUMBER " + this.a);
  26.        
  27.     }
  28.    
  29.     public void showlntB() {
  30.         System.out.println("B AS NUMBER " + this.b);
  31.     }
  32.    
  33.    public void showBinaryA(){
  34.         System.out.println("A is binary:" + Integer.toBinaryString(this.a));
  35.    
  36.    }
  37.     public void showBinaryB(){
  38.         System.out.println("B is binary:" + Integer.toBinaryString(this.b));
  39.     }
  40.    
  41.     public int bitwiseOr() {
  42.         return this.a | this.b;
  43.        
  44.     }
  45.    
  46.     public int bitwiseAnd(){
  47.         return this.a & this.b ;
  48.      }
  49.    
  50.     public int bitwiseXor(){
  51.          return this.a ^ this.b ;  
  52.      }
  53.        
  54.    
  55.     public int getComplementOneA(){
  56.         return ~ this. a ;
  57.     }  
  58.    
  59.     public int getShiftRightOneA() {
  60.         return this.a >> 1;
  61.     }
  62.  
  63.     public int getShiftLeftOneA() {
  64.         return this.a << 1;
  65.     }
  66.    
  67.     public int getShiftRightA(int N){
  68.         return a>>N;
  69.     }
  70.    
  71.     public int setComplementOneA(){
  72.         int A=this.a;
  73.         return this.a=~A;
  74.     }
  75.    
  76.    
  77.     void setComplementOneACheck(){
  78.         System.out.println(Integer.toBinaryString(a));
  79.     }
  80. };
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. package bitwiseapp;
  89.  
  90.  
  91. public class BitwiseApp {
  92.  
  93.    
  94.     public static void main(String[] args) {
  95.         BitwiseOperations bo = new BitwiseOperations();
  96.         bo.setA(10);
  97.         bo.setB(7);
  98.         bo.showlntA();
  99.         bo.showlntB();
  100.         bo.showBinaryA();
  101.         bo.showBinaryB();
  102.  
  103.         System.out.println(" ");
  104.  
  105.         int k = bo.bitwiseOr();
  106.         System.out.println("Bitwise or as integer " + k);
  107.         bo.showBinaryA();
  108.         System.out.println("Bitwise or binary " + Integer.toBinaryString(k));
  109.         bo.showBinaryB();
  110.  
  111.         System.out.println(" ");
  112.  
  113.         int m = bo.bitwiseAnd();
  114.         System.out.println("Bitwise or as integer " + m);
  115.         bo.showBinaryA();
  116.         System.out.println("Bitwise or binary " + Integer.toBinaryString(m));
  117.         bo.showBinaryB();
  118.  
  119.         System.out.println(" ");
  120.  
  121.         int n = bo.bitwiseXor();
  122.         System.out.println("Bitwise or as integer " + n);
  123.         bo.showBinaryA();
  124.         System.out.println("Bitwise or binary " + Integer.toBinaryString(n));
  125.         bo.showBinaryB();
  126.  
  127.         bo.showBinaryA();
  128.         int printComplement = bo.getComplementOneA();
  129.         System.out.println(Integer.toBinaryString(printComplement));
  130.  
  131.         System.out.println(" ");
  132.         bo.showBinaryA();
  133.         int printShiftRightOne = bo.getShiftRightOneA();
  134.         System.out.println(Integer.toBinaryString(printShiftRightOne));
  135.  
  136.         System.out.println(" ");
  137.         bo.showBinaryA();
  138.         int printShiftLeftOne = bo.getShiftLeftOneA();
  139.         System.out.println(Integer.toBinaryString(printShiftLeftOne));
  140.  
  141.         System.out.println(" ");
  142.         bo.showBinaryA();
  143.         int shiftRightA = bo.getShiftRightA(2);
  144.         System.out.println(Integer.toBinaryString(shiftRightA));
  145.  
  146.         System.out.println(" ");
  147.         bo.showBinaryA();
  148.         bo.setComplementOneA();
  149.  
  150.         bo.setComplementOneACheck();
  151.        
  152.     }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement