जावा बिटवाइज़ और शिफ़्ट ऑपरेटर्स (उदाहरण के साथ)

इस ट्यूटोरियल में, हम उदाहरणों की मदद से जावा में बिटवाइज़ ऑपरेटर और विभिन्न प्रकार के शिफ्ट ऑपरेटरों के बारे में जानेंगे।

जावा में, बिटवाइज़ ऑपरेटर व्यक्तिगत बिट-स्तर पर पूर्णांक डेटा पर संचालन करते हैं। इधर, पूर्णांक डेटा शामिल हैं byte, short, int, और longडेटा के प्रकार।

जावा में बिट-स्तरीय संचालन करने के लिए 7 ऑपरेटर हैं।

ऑपरेटर विवरण
| बिटवार या
& बिटवाइज़ और
^ बिटवाइज़ XOR
~ बिटवाइज़ पूरक
<< बायां शिफ्ट
>> राइट शिफ्ट पर हस्ताक्षर किए
>>> अहस्ताक्षरित राइट शिफ्ट

1. जावा बिटवाइज या ऑपरेटर

बिटवाइज या |ऑपरेटर 1 लौटाता है यदि कम से कम एक ऑपरेंड 1. 1 है, अन्यथा, यह 0 देता है।

निम्न सत्य तालिका बिटवाइज या ऑपरेटर के काम को प्रदर्शित करती है। चलो और बी दो ऑपरेंड हो सकते हैं जो केवल बाइनरी मान ले सकते हैं अर्थात 1 या 0।

बी ए | बी
1 है 1 है
1 है 1 है
1 है 1 है 1 है

उपरोक्त तालिका को बिटवाइज़ या ऑपरेटर के लिए "ट्रूथ टेबल" के रूप में जाना जाता है।

आइए दो पूर्णांक 12 और 25 के बिटवाइज या ऑपरेशन को देखें।

 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise OR Operation of 12 and 25 00001100 | 00011001 ____________ 00011101 = 29 (In Decimal)

उदाहरण 1: बिटवाइज़ या

 class Main ( public static void main(String() args) ( int number1 = 12, number2 = 25, result; // bitwise OR between 12 and 25 result = number1 | number2; System.out.println(result); // prints 29 ) )

2. जावा बिटवाइज और ऑपरेटर

बिटवाइज़ और &ऑपरेटर 1 लौटाता है यदि और केवल यदि दोनों ऑपरेंड्स 1 हैं। अन्यथा, यह 0 देता है।

निम्न तालिका बिटवाइड और ऑपरेटर के काम को प्रदर्शित करती है। चलो और बी दो ऑपरेंड हो सकते हैं जो केवल बाइनरी मान ले सकते हैं अर्थात 1 और 0।

बी ए और बी
1 है
1 है
1 है 1 है 1 है

आइए दो पूर्णांक 12 और 25 के बिटवाइज़ और ऑपरेशन पर एक नज़र डालें।

 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) // Bitwise AND Operation of 12 and 25 00001100 & 00011001 ____________ 00001000 = 8 (In Decimal)

उदाहरण 2: बिटवाइज़ और

  class Main ( public static void main(String() args) ( int number1 = 12, number2 = 25, result; // bitwise AND between 12 and 25 result = number1 & number2; System.out.println(result); // prints 8 ) )

3. जावा बिटवाइज XOR ऑपरेटर

बिटवाइज़ XOR ^ऑपरेटर 1 लौटाता है और यदि केवल एक ऑपरेंड 1 है। हालाँकि, यदि दोनों ऑपरेंड 0 हैं या यदि दोनों 1 हैं, तो परिणाम 0 है।

निम्न सत्य तालिका बिटवाइज़र XOR ऑपरेटर के कार्य को प्रदर्शित करती है। चलो और बी दो ऑपरेंड हो सकते हैं जो केवल बाइनरी मान ले सकते हैं अर्थात 1 या 0।

बी ए और बी
1 है 1 है
1 है 1 है
1 है 1 है

आइए दो पूर्णांक 12 और 25 के बिटवाइयर XOR ऑपरेशन को देखें।

 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) // Bitwise XOR Operation of 12 and 25 00001100 00011001 ____________ 00010101 = 21 (In Decimal)

उदाहरण 4: बिटवाइज़ XOR

 class Main ( public static void main(String() args) ( int number1 = 12, number2 = 25, result; // bitwise XOR between 12 and 25 result = number1 number2; System.out.println(result); // prints 21 ) )

4. जावा बिटवाइज पूरक परिचालक

बिटवाइज़ पूरक ऑपरेटर एक यूनीरी ऑपरेटर (केवल एक ऑपरेंड के साथ काम करता है) है। इसके द्वारा निरूपित किया जाता है ~

यह बाइनरी अंकों को 1 से 0 और 0 से 1 में बदलता है ।

जावा बिटवाइज पूरक परिचालक

It is important to note that the bitwise complement of any integer N is equal to - (N + 1). For example,

Consider an integer 35. As per the rule, the bitwise complement of 35 should be -(35 + 1) = -36. Now let's see if we get the correct answer or not.

 35 = 00100011 (In Binary) // using bitwise complement operator ~ 00100011 __________ 11011100

In the above example, we get that the bitwise complement of 00100011 (35) is 11011100. Here, if we convert the result into decimal we get 220.

However, it is important to note that we cannot directly convert the result into decimal and get the desired output. This is because the binary result 11011100 is also equivalent to -36.

To understand this we first need to calculate the binary output of -36.

2's Complement

In binary arithmetic, we can calculate the binary negative of an integer using 2's complement.

1's complement changes 0 to 1 and 1 to 0. And, if we add 1 to the result of the 1's complement, we get the 2's complement of the original number. For example,

 // compute the 2's complement of 36 36 = 00100100 (In Binary) 1's complement = 11011011 2's complement: 11011011 + 1 _________ 11011100

यहां, हम देख सकते हैं कि 2 का 36 (यानी -36 ) का पूरक 11011100 है । यह मान 35 के बिटवाइज़ पूरक के बराबर है ।

इसलिए, हम कह सकते हैं कि 35 का बिटवाइज पूरक है - (35 + 1) = -36

उदाहरण 3: बिटवाइज़ पूरक

 class Main ( public static void main(String() args) ( int number = 35, result; // bitwise complement of 35 result = ~number; System.out.println(result); // prints -36 ) )

जावा शिफ्ट ऑपरेटर्स

जावा में तीन प्रकार के शिफ्ट ऑपरेटर हैं:

  • हस्ताक्षरित वाम शिफ्ट (<<)
  • हस्ताक्षरित राइट शिफ्ट (>>)
  • अहस्ताक्षरित राइट शिफ्ट (>>>)

5. जावा लेफ्ट शिफ्ट ऑपरेटर

बाएं शिफ्ट ऑपरेटर एक निश्चित संख्या में निर्दिष्ट बिट्स द्वारा बाईं ओर सभी बिट्स को स्थानांतरित करता है। इसके द्वारा निरूपित किया जाता है <<

जावा 1 बिट लेफ्ट शिफ्ट ऑपरेटर

As we can see from the image above, we have a 4-digit number. When we perform a 1 bit left shift operation on it, each individual bit is shifted to the left by 1 bit.

As a result, the left-most bit (most-significant) is discarded and the right-most position(least-significant) remains vacant. This vacancy is filled with 0s.

Example 5: Left Shift Operators

 class Main ( public static void main(String() args) ( int number = 2; // 2 bit left shift operation int result = number << 2; System.out.println(result); // prints 8 ) )

5. Java Signed Right Shift Operator

The signed right shift operator shifts all bits towards the right by a certain number of specified bits. It is denoted by >>.

When we shift any number to the right, the least significant bits (rightmost) are discarded and the most significant position (leftmost) is filled with the sign bit. For example,

 // right shift of 8 8 = 1000 (In Binary) // perform 2 bit right shift 8>> 2: 1000>> 2 = 0010 (equivalent to 2)

यहां, हम 8 की सही पारी का प्रदर्शन कर रहे हैं (यानी संकेत सकारात्मक है)। इसलिए, कोई संकेत बिट नहीं है। तो सबसे बाईं ओर 0 भरे हुए हैं (सकारात्मक संकेत का प्रतिनिधित्व करता है)।

 // right shift of -8 8 = 1000 (In Binary) 1's complement = 0111 2's complement: 0111 + 1 _______ 1000 Signed bit = 1 // perform 2 bit right shift 8>> 2: 1000>> 2 = 1110 (equivalent to -2)

यहां, हमने बाईं ओर के बिट्स को भरने के लिए हस्ताक्षरित बिट 1 का उपयोग किया है ।

उदाहरण 6: हस्ताक्षरित राइट शिफ्ट ऑपरेटर

 class Main ( public static void main(String() args) ( int number1 = 8; int number2 = -8; // 2 bit signed right shift System.out.println(number1>> 2); // prints 2 System.out.println(number2>> 2); // prints -2 ) )

7. जावा अनसाइनड राइट शिफ्ट ऑपरेटर

जावा एक अहस्ताक्षरित सही बदलाव भी प्रदान करता है। इसके द्वारा निरूपित किया जाता है >>>

यहां, संकेत के बिट के बजाय रिक्त बाईं ओर की स्थिति 0 से भरी हुई है । उदाहरण के लिए,

 // unsigned right shift of 8 8 = 1000 8>>> 2 = 0010 // unsigned right shift of -8 -8 = 1000 (see calculation above) -8>>> 2 = 0010

उदाहरण 7: अनसाइनड राइट शिफ्ट

 class Main ( public static void main(String() args) ( int number1 = 8; int number2 = -8; // 2 bit signed right shift System.out.println(number1>>> 2); // prints 2 System.out.println(number2>>> 2); // prints 1073741822 ) )

जैसा कि हम हस्ताक्षरित और अहस्ताक्षरित सही शिफ्ट ऑपरेटर नकारात्मक बिट्स के लिए अलग-अलग परिणाम देते हैं। अधिक जानने के लिए >> और >>> के बीच अंतर पर जाएं।

दिलचस्प लेख...