En este tutorial, aprenderemos sobre el operador bit a bit y los diferentes tipos de operadores de turno en Java con la ayuda de ejemplos.
En Java, los operadores bit a bit realizan operaciones con datos enteros a nivel de bit individual. Aquí, los datos de número entero incluye byte
, short
, int
, y long
tipos de datos.
Hay 7 operadores para realizar operaciones a nivel de bits en Java.
Operador | Descripción |
---|---|
| | O bit a bit |
& | Y bit a bit |
^ | XOR bit a bit |
~ | Complemento bit a bit |
<< | Shift izquierdo |
>> | Cambio a la derecha firmado |
>>> | Desplazamiento a la derecha sin firmar |
1. Operador Java Bitwise OR
El |
operador OR bit a bit devuelve 1 si al menos uno de los operandos es 1. De lo contrario, devuelve 0.
La siguiente tabla de verdad demuestra el funcionamiento del operador OR bit a bit. Sean ayb dos operandos que solo pueden tomar valores binarios, es decir, 1 o 0.
un | segundo | a | segundo |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
La tabla anterior se conoce como la "Tabla de verdad" para el operador OR bit a bit.
Veamos la operación OR bit a bit de dos enteros 12 y 25.
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise OR Operation of 12 and 25 00001100 | 00011001 ____________ 00011101 = 29 (In Decimal)
Ejemplo 1: OR bit a bit
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. Operador AND bit a bit de Java
El &
operador AND bit a bit devuelve 1 si y solo si ambos operandos son 1. De lo contrario, devuelve 0.
La siguiente tabla muestra el funcionamiento del operador AND bit a bit. Sean ayb dos operandos que solo pueden tomar valores binarios, es decir, 1 y 0.
un | segundo | a & b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Echemos un vistazo a la operación AND bit a bit de dos enteros 12 y 25.
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) // Bitwise AND Operation of 12 and 25 00001100 & 00011001 ____________ 00001000 = 8 (In Decimal)
Ejemplo 2: AND bit a bit
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. Operador Java Bitwise XOR
El ^
operador XOR bit a bit devuelve 1 si y solo si uno de los operandos es 1. Sin embargo, si ambos operandos son 0 o si ambos son 1, el resultado es 0.
La siguiente tabla de verdad demuestra el funcionamiento del operador XOR bit a bit. Sean ayb dos operandos que solo pueden tomar valores binarios, es decir, 1 o 0.
un | segundo | a & b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Veamos la operación XOR bit a bit de dos enteros 12 y 25.
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) // Bitwise XOR Operation of 12 and 25 00001100 00011001 ____________ 00010101 = 21 (In Decimal)
Ejemplo 4: XOR bit a bit
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. Operador de complemento de bits de Java
El operador de complemento bit a bit es un operador unario (funciona con un solo operando). Se denota por ~
.
Cambia los dígitos binarios 1 a 0 y 0 a 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
Aquí, podemos ver que el complemento a 2 de 36 (es decir, -36 ) es 11011100 . Este valor es equivalente al complemento bit a bit de 35 .
Por tanto, podemos decir que el complemento bit a bit de 35 es - (35 + 1) = -36 .
Ejemplo 3: Complemento bit a bit
class Main ( public static void main(String() args) ( int number = 35, result; // bitwise complement of 35 result = ~number; System.out.println(result); // prints -36 ) )
Operadores de cambio de Java
Hay tres tipos de operadores de turno en Java:
- Desplazamiento a la izquierda firmado (<<)
- Cambio a la derecha firmado (>>)
- Desplazamiento a la derecha sin signo (>>>)
5. Operador de desplazamiento a la izquierda de Java
El operador de desplazamiento a la izquierda desplaza todos los bits hacia la izquierda en un cierto número de bits especificados. Se denota por <<
.

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)
Aquí, estamos realizando el desplazamiento a la derecha de 8 (es decir, el signo es positivo). Por lo tanto, no hay bit de señal. Entonces, los bits más a la izquierda se rellenan con 0 (representa un signo positivo).
// 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)
Aquí, hemos utilizado el bit 1 con signo para llenar los bits más a la izquierda.
Ejemplo 6: Operador de cambio a la derecha firmado
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. Operador de desplazamiento a la derecha sin firmar de Java
Java también proporciona un desplazamiento a la derecha sin firmar. Se denota por >>>
.
Aquí, la posición vacante más a la izquierda se llena con 0 en lugar del bit de signo. Por ejemplo,
// unsigned right shift of 8 8 = 1000 8>>> 2 = 0010 // unsigned right shift of -8 -8 = 1000 (see calculation above) -8>>> 2 = 0010
Ejemplo 7: Desplazamiento a la derecha sin firmar
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 ) )
Como podemos ver, el operador de desplazamiento a la derecha con y sin signo devuelve resultados diferentes para bits negativos. Para obtener más información, visite Diferencia entre >> y >>>.