8051 I/O programming logic/ how it works
The 8051 I/O port is different from most other microcontrollers.

The I/O ports (except P0) effectively have open collector outputs with internal pull-up resistors (P0 doesn't have pull-ups).

If you write a Zero to a port pin, the pin will be pulled to ground, and is definitely an output.

If you write a One to a port pin, the transistor driving the port will be turned off, so the pull-up resistor will pull the pin High.  In this state, the pin can be considered as a High output, or it can be used as an input, as an external device can pull the pin Low, when it will be read as a Zero.  If the external device does not pull the pin Low, it will be read as a High input.

Many other microcontrollers have Data Direction Registers that determine whether the pins can be used as inputs or outputs, and may have other registers to determine whether outputs are open collector or not, and whether pull-up or pull-down resistors are active when the pin is set as an input.