[JAVA] Introduction to bit operation

Introduction to bit operation

Bit operations are the basis of a program, but to be clear, you can do anything without knowing it. However, by knowing it, you can expand the choices of programs, and there are some situations where it is convenient to actually use it. In addition, bit operations can be used in many computer languages, so once you learn them, you can use them in various environments. In this article, for those who do not know bit operations, we will explain a simple example of what bit operations are.

What is bit operation?

First of all, what is a bit?

A computer is a collection of innumerable ON / OFF switches. This switch is called a bit, and one switch is called a bit. And this switch is operated as a set of eight, and these eight groups are called bytes, and one set of groups is one byte. You may have heard that a program is a collection of 0s and 1s, but it comes from here. When notation, it is written as 00000000, and it counts as 0th bit, 1st bit, etc. from the right side.

What is bit operation?

Bit operations control the ON / OFF switching of innumerable switches. It's just a word of ON / OFF, but multiple commands are prepared for various situations.

And for ON / OFF control, basically multiples of 8 are performed together. That is, in bytes. There are various types such as 1-byte variables and 2-byte variables in each language, but all the bytes of that variable are collectively issued as an instruction.

operator

Operators are calculation symbols used when calculating + and-. Bit operations are special calculation symbols that are not used in mathematics, and there are six. Basically, the returned results are checked in the order of the 1st bit and the 2nd bit, and the result is entered in the 1st and 2nd bits, respectively.

&(AND) Compares the two switches and returns 1 if both are 1, and 0 if either one is 0. It is used to check whether the flag is set when the flag is bit-managed. Example: c = a & b a 11110000 b 10101010   ↓ c 10100000

|(OR) Compares two switches and returns 1 if either is 1 or 0 if both are 0. It is used when setting a flag when bit management is performed. Example: c = a | b a 11110000 b 10101010   ↓ c 11111010

! (NOT) * Also in the case of ~

Reverses all switch ON / OFF states, changing 0 to 1 and 1 to 0. Use this when you want to make the opposite state. Example: c =! a a 11110000   ↓ c 00001111

^(XOR) Compares two switches and returns 1 if only one of them is 1, 0 if both are 1 or 0 if both are 1. It is used when you want to find out what is different. Example: c = a ^ b a 11110000 b 10101010   ↓ c 01011010

>> (Right bit shift)

Shift the ON / OFF status of the switch by one to the right. 0 will be entered in the vacant frame due to the shift, and the part that exceeds the fixed number of digits will disappear. If it sticks out, it disappears, which is called overflow. It is used when manipulating bits using a loop. Note: c = a >> 1 a 11110000   ↓ c 01111000

<< (Left bit shift)

Shift the ON / OFF status of the switch by one to the left. Except for this part, it is almost the same as the right bit shift. Note: c = a >> 1 a 11110000   ↓ c 11100000

Summary

The above is the basis of bit operation. By combining these, it is possible to perform computer-specific calculations that cannot be done simply by +-× ÷. When you actually start using it, you often care about how many bytes the variable is, but it is worthwhile to use overflow intentionally. The world is enthusiastic about Bitcoin, but please remember the bit operation once in a while!

Recommended Posts

Introduction to bit operation
Introduction to SWING
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to migration
Introduction to java
Introduction to Doma
Introduction to JAR files
Introduction to Ratpack (8)-Session
Introduction to algorithms with java --Search (bit full search)
Bit operation speedup memo
Introduction to RSpec 1. Test, RSpec
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
Introduction to Android Layout
Introduction to design patterns (introduction)
Introduction to Practical Programming
Introduction to javadoc command
Introduction to jar command
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Introduction to java command
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Introduction to javac command
Introduction to Design Patterns (Builder)
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to Android application development
Introduction to Ratpack (5) --Json & Registry
Bit Tetris (how to write)
Introduction to Metabase ~ Environment Construction ~
Introduction to Ratpack (7) --Guice & Spring
(Dot installation) Introduction to Java8_Impression
Introduction to Design Patterns (Composite)
Introduction to Micronaut 2 ~ Unit test ~
Introduction to JUnit (study memo)
Introduction to Spring Boot ① ~ DI ~
Introduction to design patterns (Flyweight)
[Java] Introduction to lambda expressions
Introduction to Spring Boot ② ~ AOP ~
Introduction to Apache Beam (2) ~ ParDo ~
[Ruby] Introduction to Ruby Error statement
Introduction to EHRbase 2-REST API
Introduction to design patterns Prototype
GitHub Actions Introduction to self-made actions
[Java] Introduction to Stream API
Introduction to Design Patterns (Iterator)
Introduction to Spring Boot Part 1
Introduction to Ratpack (1) --What is Ratpack?
XVim2 introduction memo to Xcode12.3
Introduction to RSpec-Everyday Rails Summary-
Introduction to Design Patterns (Strategy)
[Introduction to rock-paper-scissors games] Java
Introduction to Linux Container / Docker (Part 1)
Operation to connect multiple Streams @Java
Introduction to swift practice output Chapter5
[Introduction to Java] About lambda expressions
[Introduction to Java] About Stream API