Difference between revisions of "JAVA: Operator Bitwise di Java"

From OnnoWiki
Jump to navigation Jump to search
Line 2: Line 2:
 
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types:  
 
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types:  
  
* Operator Arithmetic Operators
+
Operator merupakan blok bangunan dasar untuk bahasa pemrograman apa pun. Java juga menyediakan banyak jenis operator yang dapat digunakan sesuai dengan kebutuhan untuk melakukan berbagai perhitungan dan fungsi, baik itu logika, aritmatika, relasional, dll. Diklasifikasikan berdasarkan fungsionalitas yang disediakan. Berikut beberapa jenisnya:
* Operator Unary Operators
+
 
* Operator Assignment Operator
+
 
* Operator Relational Operators
+
* Operator Arithmetic
* Operator Logical Operators
+
* Operator Unary
* Operator Ternary Operator
+
* Operator Assignment
* Operator Bitwise Operators
+
* Operator Relational
* Operator Shift Operators
+
* Operator Logical
 +
* Operator Ternary
 +
* Operator Bitwise
 +
* Operator Shift
  
 
This article explains all that one needs to know regarding Bitwise Operators.  
 
This article explains all that one needs to know regarding Bitwise Operators.  
 +
 +
Artikel ini menjelaskan semua yang perlu diketahui tentang Operator Bitwise.
 +
  
 
==Operator Bitwise==
 
==Operator Bitwise==
  
 
Bitwise operators are used to perform the manipulation of individual bits of a number. They can be used with any integral type (char, short, int, etc.). They are used when performing update and query operations of the Binary indexed trees.  
 
Bitwise operators are used to perform the manipulation of individual bits of a number. They can be used with any integral type (char, short, int, etc.). They are used when performing update and query operations of the Binary indexed trees.  
 +
 +
Operator bitwise digunakan untuk melakukan manipulasi bit individu dari suatu angka. Mereka dapat digunakan dengan semua tipe integral (char, short, int, dll.). Mereka digunakan saat melakukan pembaruan dan operasi kueri pohon berindeks Biner.
  
 
Now let’s look at each one of the bitwise operators in Java:  
 
Now let’s look at each one of the bitwise operators in Java:  
 +
 +
Sekarang mari kita lihat masing-masing operator bitwise di Java:
  
 
==1. Bitwise OR (|)==
 
==1. Bitwise OR (|)==
  
 
This operator is a binary operator, denoted by ‘|’. It returns bit by bit OR of input values, i.e., if either of the bits is 1, it gives 1, else it shows 0.  
 
This operator is a binary operator, denoted by ‘|’. It returns bit by bit OR of input values, i.e., if either of the bits is 1, it gives 1, else it shows 0.  
 +
 +
Operator ini adalah operator biner, dilambangkan dengan '|'. Ini mengembalikan bit demi bit ATAU nilai input, yaitu, jika salah satu bit adalah 1, ia memberikan 1, selain itu menunjukkan 0.
  
 
Contoh:
 
Contoh:
Line 29: Line 41:
  
 
Bitwise OR Operation of 5 and 7
 
Bitwise OR Operation of 5 and 7
 +
 +
Bitwise OR Operasi 5 dan 7
 
   
 
   
 
   0101
 
   0101
Line 38: Line 52:
  
 
This operator is a binary operator, denoted by ‘&.’ It returns bit by bit AND of input values, i.e., if both bits are 1, it gives 1, else it shows 0.  
 
This operator is a binary operator, denoted by ‘&.’ It returns bit by bit AND of input values, i.e., if both bits are 1, it gives 1, else it shows 0.  
 +
 +
Operator ini adalah operator biner, dilambangkan dengan 'dan.' Ini mengembalikan bit demi bit DAN nilai input, i.n., jika kedua bit adalah 1, ia memberikan 1, jika tidak menunjukkan 0.
 +
  
 
Contoh:
 
Contoh:
Line 45: Line 62:
  
 
Bitwise AND Operation of 5 and 7
 
Bitwise AND Operation of 5 and 7
 +
 +
Bitwise AND Operasi 5 dan 7
  
 
   0101
 
   0101
Line 54: Line 73:
  
 
This operator is a binary operator, denoted by ‘^.’ It returns bit by bit XOR of input values, i.e., if corresponding bits are different, it gives 1, else it shows 0.  
 
This operator is a binary operator, denoted by ‘^.’ It returns bit by bit XOR of input values, i.e., if corresponding bits are different, it gives 1, else it shows 0.  
 +
 +
Operator ini adalah operator biner, dilambangkan dengan '^.' Ini mengembalikan bit demi bit XOR dari nilai input, yaitu, jika bit yang sesuai berbeda, memberikan 1, selain itu menunjukkan 0.
 +
  
 
Contoh:
 
Contoh:
Line 61: Line 83:
  
 
Bitwise XOR Operation of 5 and 7
 
Bitwise XOR Operation of 5 and 7
 +
 +
Operasi XOR Bitwise 5 dan 7
  
 
   0101
 
   0101
Line 70: Line 94:
  
 
This operator is a unary operator, denoted by ‘~.’ It returns the one’s complement representation of the input value, i.e., with all bits inverted, which means it makes every 0 to 1, and every 1 to 0.  
 
This operator is a unary operator, denoted by ‘~.’ It returns the one’s complement representation of the input value, i.e., with all bits inverted, which means it makes every 0 to 1, and every 1 to 0.  
 +
 +
Operator ini adalah operator unary, dilambangkan dengan '~.' Ini mengembalikan representasi komplemen satu dari nilai input, yaitu, dengan semua bit terbalik, yang berarti membuat setiap 0 menjadi 1, dan setiap 1 menjadi 0.
 +
  
 
Contoh:
 
Contoh:
Line 75: Line 102:
 
  a = 5 = 0101 (In Binary)
 
  a = 5 = 0101 (In Binary)
  
Bitwise Complement Operation of 5
+
Operasi Bitwise Complement dari 5
  
 
  ~ 0101
 
  ~ 0101
Line 126: Line 153:
 
  a= 5
 
  a= 5
  
==Bit-Shift Operators (Shift Operators)==
+
==Operator Bit-Shift (Operator Shift)==
  
 
Shift operators are used to shift the bits of a number left or right, thereby multiplying or dividing the number by two, respectively. They can be used when we have to multiply or divide a number by two.  
 
Shift operators are used to shift the bits of a number left or right, thereby multiplying or dividing the number by two, respectively. They can be used when we have to multiply or divide a number by two.  
 +
 +
Operator shift digunakan untuk menggeser bit angka ke kiri atau kanan, sehingga mengalikan atau membagi angka dengan dua, masing-masing. Mereka dapat digunakan ketika kita harus mengalikan atau membagi angka dengan dua.
 +
  
 
Sintaks:  
 
Sintaks:  
Line 134: Line 164:
 
  number shift_op number_of_places_to_shift;
 
  number shift_op number_of_places_to_shift;
  
Types of Shift Operators:
+
Tipe dari Operator Shift:
  
Shift Operators are further divided into 4 types. These are:
+
Shift Operator dibagi menjadi 4 jenis, yaitu:
  
  Signed Right shift operator (>>)
+
  Operator Signed Right shift (>>)
  Unsigned Right shift operator (>>>)
+
  Operator Unsigned Right shift (>>>)
  Left shift operator
+
  Operator Left shift
  Unsigned Left shift operator (<<<)
+
  Operator Unsigned Left shift (<<<)
  
 
Note: For more detail about the Shift Operators in Java, refer Shift Operator in Java.
 
Note: For more detail about the Shift Operators in Java, refer Shift Operator in Java.

Revision as of 06:43, 7 May 2022

Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types:

Operator merupakan blok bangunan dasar untuk bahasa pemrograman apa pun. Java juga menyediakan banyak jenis operator yang dapat digunakan sesuai dengan kebutuhan untuk melakukan berbagai perhitungan dan fungsi, baik itu logika, aritmatika, relasional, dll. Diklasifikasikan berdasarkan fungsionalitas yang disediakan. Berikut beberapa jenisnya:


  • Operator Arithmetic
  • Operator Unary
  • Operator Assignment
  • Operator Relational
  • Operator Logical
  • Operator Ternary
  • Operator Bitwise
  • Operator Shift

This article explains all that one needs to know regarding Bitwise Operators.

Artikel ini menjelaskan semua yang perlu diketahui tentang Operator Bitwise.


Operator Bitwise

Bitwise operators are used to perform the manipulation of individual bits of a number. They can be used with any integral type (char, short, int, etc.). They are used when performing update and query operations of the Binary indexed trees.

Operator bitwise digunakan untuk melakukan manipulasi bit individu dari suatu angka. Mereka dapat digunakan dengan semua tipe integral (char, short, int, dll.). Mereka digunakan saat melakukan pembaruan dan operasi kueri pohon berindeks Biner.

Now let’s look at each one of the bitwise operators in Java:

Sekarang mari kita lihat masing-masing operator bitwise di Java:

1. Bitwise OR (|)

This operator is a binary operator, denoted by ‘|’. It returns bit by bit OR of input values, i.e., if either of the bits is 1, it gives 1, else it shows 0.

Operator ini adalah operator biner, dilambangkan dengan '|'. Ini mengembalikan bit demi bit ATAU nilai input, yaitu, jika salah satu bit adalah 1, ia memberikan 1, selain itu menunjukkan 0.

Contoh:

a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise OR Operation of 5 and 7

Bitwise OR Operasi 5 dan 7

  0101
| 0111
 ________
  0111  = 7 (In decimal) 

2. Bitwise AND (&)

This operator is a binary operator, denoted by ‘&.’ It returns bit by bit AND of input values, i.e., if both bits are 1, it gives 1, else it shows 0.

Operator ini adalah operator biner, dilambangkan dengan 'dan.' Ini mengembalikan bit demi bit DAN nilai input, i.n., jika kedua bit adalah 1, ia memberikan 1, jika tidak menunjukkan 0.


Contoh:

a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise AND Operation of 5 and 7

Bitwise AND Operasi 5 dan 7

  0101
& 0111
 ________
  0101  = 5 (In decimal) 

3. Bitwise XOR (^)

This operator is a binary operator, denoted by ‘^.’ It returns bit by bit XOR of input values, i.e., if corresponding bits are different, it gives 1, else it shows 0.

Operator ini adalah operator biner, dilambangkan dengan '^.' Ini mengembalikan bit demi bit XOR dari nilai input, yaitu, jika bit yang sesuai berbeda, memberikan 1, selain itu menunjukkan 0.


Contoh:

a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise XOR Operation of 5 and 7

Operasi XOR Bitwise 5 dan 7

  0101
^ 0111
 ________
  0010  = 2 (In decimal) 

4. Bitwise Complement (~)

This operator is a unary operator, denoted by ‘~.’ It returns the one’s complement representation of the input value, i.e., with all bits inverted, which means it makes every 0 to 1, and every 1 to 0.

Operator ini adalah operator unary, dilambangkan dengan '~.' Ini mengembalikan representasi komplemen satu dari nilai input, yaitu, dengan semua bit terbalik, yang berarti membuat setiap 0 menjadi 1, dan setiap 1 menjadi 0.


Contoh:

a = 5 = 0101 (In Binary)

Operasi Bitwise Complement dari 5

~ 0101
 ________
  1010  = 10 (In decimal) 

Note: Compiler will give 2’s complement of that number, i.e., 2’s complement of 10 will be -6.

// Java program to illustrate
// bitwise operators
 
public class operators {
    public static void main(String[] args)
    {
        // Initial values
        int a = 5;
        int b = 7;
 
        // bitwise and
        // 0101 & 0111=0101 = 5
        System.out.println("a&b = " + (a & b));
 
        // bitwise or
        // 0101 | 0111=0111 = 7
        System.out.println("a|b = " + (a | b));
 
        // bitwise xor
        // 0101 ^ 0111=0010 = 2
        System.out.println("a^b = " + (a ^ b));
 
        // bitwise not
        // ~0101=1010
        // will give 2's complement of 1010 = -6
        System.out.println("~a = " + ~a);

        // can also be combined with
        // assignment operator to provide shorthand
        // assignment
        // a=a&b
        a &= b;
        System.out.println("a= " + a);
    }
}

Output

a&b = 5
a|b = 7
a^b = 2
~a = -6
a= 5

Operator Bit-Shift (Operator Shift)

Shift operators are used to shift the bits of a number left or right, thereby multiplying or dividing the number by two, respectively. They can be used when we have to multiply or divide a number by two.

Operator shift digunakan untuk menggeser bit angka ke kiri atau kanan, sehingga mengalikan atau membagi angka dengan dua, masing-masing. Mereka dapat digunakan ketika kita harus mengalikan atau membagi angka dengan dua.


Sintaks:

number shift_op number_of_places_to_shift;

Tipe dari Operator Shift:

Shift Operator dibagi menjadi 4 jenis, yaitu:

Operator Signed Right shift (>>)
Operator Unsigned Right shift (>>>)
Operator Left shift
Operator Unsigned Left shift (<<<)

Note: For more detail about the Shift Operators in Java, refer Shift Operator in Java.


Referensi