[Python] What is happening during this operation 0x99<<j & (0x80>>i))

Discussão em 'Python' iniciado por Stack, Julho 2, 2025.

  1. Stack

    Stack Membro Participativo

    i've been tinkering around, and i have googled and searched about bitwise operations. and i think i understand some of it, however i have a piece of code i have copied from someone online which allows it to move a 28byj-48 5v motor and driver in python. its actually the only code i've found in python that it would allow it to work.

    however, i tried breaking it down with my own code and cant seem to figure out why mine wont work but this one does.

    here it is, the working one:

    import RPi.GPIO as GPIO
    from time import sleep

    #configuring pins and motors
    motorPin = (18,23,24,25) #GPIO pin in BCM mode refer to map
    rolePerMinute =13
    stepsPerRevolution = 2048
    stepSpeed = (60/rolePerMinute)/stepsPerRevolution


    #setup the pins to the motor
    def setup():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)

    for i in motorPin:
    GPIO.setup(i, GPIO.OUT)



    def rotary1():
    n =1
    while n < 10000:
    for j in range(4):
    for i in range(4):
    GPIo_Output(motorPin,0x99>>j & (0x08>>i))
    sleep(stepSpeed)
    n +=1


    and here is mine:


    import RPi.GPIO as GPIO
    from time import sleep

    #configuring pins and motors
    motorPin = (18,23,24,25) #GPIO pin in BCM mode refer to map
    rolePerMinute =13
    stepsPerRevolution = 2048
    stepSpeed = (60/rolePerMinute)/stepsPerRevolution


    #setup the pins to the motor
    def setup():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)

    for i in motorPin:
    GPIO.setup(i, GPIO.OUT)

    def rotary():
    n = 1
    while n < 10000:

    GPIo_Output(18,0x99>>1 & (0x08>>1))
    sleep(stepSpeed)
    GPIo_Output(23,0x99>>2 & (0x08>>2))
    sleep(stepSpeed)
    GPIo_Output(24,0x99>>3 & (0x08>>3))
    sleep(stepSpeed)
    GPIo_Output(25,0x99>>4 & (0x08>>4))
    sleep(stepSpeed)
    n+= 1


    if im understanding this correctly, 0X99 is transformed into a bit "10011001" and its ">>" bit wise operator is pushing it by 1 to the left?

    this is where im confused, and also why wouldn't my stepped version work versus the first version.

    if someone could help me understand this, i would be greatful.

    many thanks.

    i've tried googling how bitwise work and watched a very informative video, however i still couldnt understand how << or >> works in this case with Hex values. I also couldn't get my stepped version to work, versus the other.

    Continue reading...

Compartilhe esta Página