how to read rotary encoder c++

#include <iostream>
#include <wiringPi.h>

int main() {
    wiringPiSetup();
    int pinA = 0; // replace 0 with the GPIO pin connected to A
    int pinB = 1; // replace 1 with the GPIO pin connected to B
    int lastEncoded = 0;
    int encoderValue = 0;
    int value = 0;
    while (true) {
        int MSB = digitalRead(pinA);
        int LSB = digitalRead(pinB);
        int encoded = (MSB << 1) | LSB;
        int sum = (lastEncoded << 2) | encoded;
        if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) {
            encoderValue++;
        } else if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) {
            encoderValue--;
        }
        lastEncoded = encoded;
        std::cout << "Encoder Value: " << encoderValue << std::endl;
    }
    return 0;
}