Contenido del Curso
C++ Introduction
4. Introduction to Program Flow
5. Introduction to Functions
C++ Introduction
Bitwise Shift Operators
What is a byte
This is a byte, a fundamental unit of digital information storage. A byte consists of 8 bits.
At the core of digital information storage is the byte, composed of 8 bits. Each bit represents a binary digit, and numbers are commonly expressed in the binary system, a base-2 numeral system. The binary representation involves expressing a number as a sum of powers of 2.
Binary Representation Examples
Consider the number 4, which in binary is represented as 00000100. Each bit in this byte corresponds to a power of 2:
Similarly, the number 5 is represented as 00000101:
Bitwise shift
- In this context, a single left bit shift effectively multiplies the value by 2;
- Conversely, when we execute a single right bit shift, divides the value by 2.
Think of the right and left shift operation >>
OR <<
by n positions as a division by 2^n. In the case of 10 >> 3, it is effectively dividing 10 by 2^3 (which is 8), and the result is 1.
main.cpp
Note
Try shifting value to the left (
<<
) or right (>>
) by different positions.
¿Todo estuvo claro?
Contenido del Curso
C++ Introduction
4. Introduction to Program Flow
5. Introduction to Functions
C++ Introduction
Bitwise Shift Operators
What is a byte
This is a byte, a fundamental unit of digital information storage. A byte consists of 8 bits.
At the core of digital information storage is the byte, composed of 8 bits. Each bit represents a binary digit, and numbers are commonly expressed in the binary system, a base-2 numeral system. The binary representation involves expressing a number as a sum of powers of 2.
Binary Representation Examples
Consider the number 4, which in binary is represented as 00000100. Each bit in this byte corresponds to a power of 2:
Similarly, the number 5 is represented as 00000101:
Bitwise shift
- In this context, a single left bit shift effectively multiplies the value by 2;
- Conversely, when we execute a single right bit shift, divides the value by 2.
Think of the right and left shift operation >>
OR <<
by n positions as a division by 2^n. In the case of 10 >> 3, it is effectively dividing 10 by 2^3 (which is 8), and the result is 1.
main.cpp
Note
Try shifting value to the left (
<<
) or right (>>
) by different positions.
¿Todo estuvo claro?