Hướng dẫn what are different bitwise operators in php explain? - giải thích các toán tử bitwise khác nhau trong php là gì?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết


    Đọc

    • Bàn luận This is a binary operator i.e. it works on two operand. Bitwise AND operator in PHP takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

      Các toán tử bitwise được sử dụng để thực hiện các hoạt động cấp độ bit trên các toán hạng. Các toán tử trước tiên được chuyển đổi thành cấp độ bit và sau đó tính toán được thực hiện trên các toán hạng. Các hoạt động toán học như bổ sung, trừ, nhân, vv có thể được thực hiện ở cấp độ bit để xử lý nhanh hơn. Trong PHP, các toán tử hoạt động ở cấp độ bit là::

      $First & $Second
      
      This will return another number whose bits are 
      set if both the bit of first and second are set.
      

      Example:

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
    • & [Bitwise và]: Đây là một toán tử nhị phân, tức là nó hoạt động trên hai toán hạng. BitWise và toán tử trong PHP lấy hai số làm toán hạng và thực hiện và trên mỗi bit của hai số. Kết quả và chỉ là 1 nếu cả hai bit là 1. This is also binary operator i.e. works on two operand. Bitwise OR operator takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.

      Syntax::

      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      

      Example:

      Input: First = 5, Second = 3
      
      Output: The bitwise | of both these value will be 7. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011.
      Therefore their bitwise | will be 0111 [i.e. set 
      if either first or second have their bit set.]
      
    • Cú pháp: This is also binary operator i.e. works on two operand. This is also known as Exclusive OR operator. Bitwise XOR takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

      Syntax::

      $First ^ $Second
      
      This will return another number whose bits are 
      set if one of the bit in first or second is 
      set but not both.
      

      Example:

      Input: First = 5, Second = 3 
      
      Output: The bitwise ^ of both these value will be 6. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise ^ will be 0110 [i.e. set 
      if either first or second have their bit set but 
      not both.]
      
    • | [BitWise hoặc]: Đây cũng là toán tử nhị phân, tức là hoạt động trên hai toán hạng. BitWise hoặc toán tử lấy hai số làm toán hạng và thực hiện hoặc trên mỗi bit của hai số. Kết quả của hoặc là 1 bất kỳ trong hai bit là 1. : This is a unary operator i.e. works on only one operand. Bitwise NOT operator takes one number and inverts all bits of it.

      Syntax::

      ~$number
      
      This will invert all the bits of $number.
      

      Example:

      Input: number = 5
      
      Output: The bitwise '~' of this number will be -6.
      
      Explanation:
      Binary representation of 5 is 0101. Therefore the
      bitwise ~ of this will be 1010 [inverts all the 
      bits of the input number]
      
    • ^ [BitWise XOR]: Đây cũng là toán tử nhị phân, tức là hoạt động trên hai toán hạng. Điều này còn được gọi là độc quyền hoặc nhà điều hành. Bitwise XOR lấy hai số làm toán hạng và thực hiện XOR trên mỗi bit của hai số. Kết quả của XOR là 1 nếu hai bit khác nhau. This is a binary operator i.e. works on two operand. Bitwise Left Shift operator takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.

      Syntax::

      $First > [BitWise Shift Shift]: Đây cũng là toán tử nhị phân, tức là hoạt động trên hai toán hạng. Toán tử BitWise Shift có hai số, phải thay đổi các bit của toán hạng đầu tiên, toán hạng thứ hai quyết định số lượng địa điểm thay đổi.

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      3

      Lưu ý: Bitwise Shift bên phải với một bit tương đương với phân chia với 2.

      Dưới đây là việc triển khai các toán tử bitwise trong PHP:

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      5
      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      6

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      8
      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      9

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      8
      Input: First = 5, Second = 3
      
      Output: The bitwise | of both these value will be 7. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011.
      Therefore their bitwise | will be 0111 [i.e. set 
      if either first or second have their bit set.]
      
      3
      Input: First = 5, Second = 3
      
      Output: The bitwise | of both these value will be 7. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011.
      Therefore their bitwise | will be 0111 [i.e. set 
      if either first or second have their bit set.]
      
      0

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      1
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      2
      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      5
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      4
      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      8
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      6

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      8
      $First ^ $Second
      
      This will return another number whose bits are 
      set if one of the bit in first or second is 
      set but not both.
      
      4
      Input: First = 5, Second = 3
      
      Output: The bitwise | of both these value will be 7. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011.
      Therefore their bitwise | will be 0111 [i.e. set 
      if either first or second have their bit set.]
      
      0

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      8
      Input: First = 5, Second = 3
      
      Output: The bitwise | of both these value will be 7. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011.
      Therefore their bitwise | will be 0111 [i.e. set 
      if either first or second have their bit set.]
      
      3
      Input: First = 5, Second = 3
      
      Output: The bitwise | of both these value will be 7. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011.
      Therefore their bitwise | will be 0111 [i.e. set 
      if either first or second have their bit set.]
      
      0

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      8
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      9
      Input: First = 5, Second = 3
      
      Output: The bitwise | of both these value will be 7. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011.
      Therefore their bitwise | will be 0111 [i.e. set 
      if either first or second have their bit set.]
      
      0

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      8
      Input: First = 5, Second = 3 
      
      Output: The bitwise ^ of both these value will be 6. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise ^ will be 0110 [i.e. set 
      if either first or second have their bit set but 
      not both.]
      
      9
      Input: First = 5, Second = 3
      
      Output: The bitwise | of both these value will be 7. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011.
      Therefore their bitwise | will be 0111 [i.e. set 
      if either first or second have their bit set.]
      
      0

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      8
      Input: First = 5, Second = 3
      
      Output: The bitwise | of both these value will be 7. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011.
      Therefore their bitwise | will be 0111 [i.e. set 
      if either first or second have their bit set.]
      
      3
      Input: First = 5, Second = 3
      
      Output: The bitwise | of both these value will be 7. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011.
      Therefore their bitwise | will be 0111 [i.e. set 
      if either first or second have their bit set.]
      
      0

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      1
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      22
      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      5
      Input: First = 5, Second = 3
      
      Output: The bitwise | of both these value will be 7. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011.
      Therefore their bitwise | will be 0111 [i.e. set 
      if either first or second have their bit set.]
      
      9
      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      8
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      6

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      8
      Input: number = 5
      
      Output: The bitwise '~' of this number will be -6.
      
      Explanation:
      Binary representation of 5 is 0101. Therefore the
      bitwise ~ of this will be 1010 [inverts all the 
      bits of the input number]
      
      2
      Input: First = 5, Second = 3
      
      Output: The bitwise | of both these value will be 7. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011.
      Therefore their bitwise | will be 0111 [i.e. set 
      if either first or second have their bit set.]
      
      0

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      8
      Input: First = 5, Second = 3
      
      Output: The bitwise | of both these value will be 7. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011.
      Therefore their bitwise | will be 0111 [i.e. set 
      if either first or second have their bit set.]
      
      3
      Input: First = 5, Second = 3
      
      Output: The bitwise | of both these value will be 7. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011.
      Therefore their bitwise | will be 0111 [i.e. set 
      if either first or second have their bit set.]
      
      0

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      1
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      2
      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      5
      Input: First = 5, Second = 3 
      
      Output: The bitwise ^ of both these value will be 6. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise ^ will be 0110 [i.e. set 
      if either first or second have their bit set but 
      not both.]
      
      4
      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      8
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      6

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      1
      ~$number
      
      This will invert all the bits of $number.
      
      7
      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      5
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      6

      Input: $First = 5,  $Second = 3
      
      Output: The bitwise & of both these value will be 1. 
      
      Explanation:
      Binary representation of 5 is 0101 and 3 is 0011. 
      Therefore their bitwise & will be 0001 [i.e. set 
      if both first and second have their bit set.]
      
      4
      $First | $Second
      
      This will return another number whose bits are 
      set if either the bit of first or second are set.
      
      8
      Input: First = 5, Second = 1
      
      Output: The bitwise 

    Bài Viết Liên Quan

    Chủ Đề