• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Bitwise Operators in Java

April 14, 2022 Java

In this post, you will learn how to work with bitwise operators so that you can use that same knowledge and thus evolve.

  • bitwise operators operate one bit at a time and it is important to be aware of what binary code is.
1 Real
0 False
& Only true when both numbers correspond to 1 and 1
| It is only false when both numbers match 0 and 0
<< Shift bits according to given number to the left
>> Shift bits according to given number to the right
  • then we also have the operators ^ and ~;
  • such operators are mostly used in various situations (encryption, data compression, production of hash codes, communication with hardware, games and etc).
package com.caffeinealgorithm.programminginjava;

public class BitwiseOperators {
  private int x = 60; // 00111100 < 01111000 < 11110000 | 00111100 > 00011110 > 00001111
  private int y = 13; // 00001101

  public void Run() {
    System.out.printf("Result of operator &: %d\n", x & y); // 12 -> 00001100
    System.out.printf("Result of operator |: %d\n", x | y); // 61 -> 00111101
    System.out.printf("Result of operator <<: %d\n", x << 2); // 240 -> 11110000
    System.out.printf("Result of operator >>: %d", x >> 2); // 15 -> 00001111
  }
}

/*
  Result of operator &: 12
  Result of operator |: 61
  Result of operator <<: 240
  Result of operator >>: 15
*/

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved