In this paper, we present an all-optical XOR gate based on two-dimensional photonic crystals (PCs). The proposed gate is designed for use in optical computing and all-optical logic applications, offering advantages such as fast computation and parallel processing. To enhance the design process and optimize its performance, we employ an artificial neural network (ANN) to model the gate’s behavior. Subsequently, a particle swarm optimization (PSO) algorithm is applied to refine the necessary parameters, ultimately leading to an optimized structure that enhances the gate’s performance. The outputs of the structure are obtained using the finite-difference time-domain (FDTD) method, with approximately 555 data samples utilized in the PSO framework to enhance structure optimization and prediction. The proposed all-optical XOR gate exhibits a range of output powers across the respective logical states (desired state): 0 (0), 0.995 (1), 0.935 (1), and 0.015 (0). The proposed structure achieves an acceptable contrast ratio of 18.21 dB. Using ANN-PSO approach for improving the performance of PCs is a beneficial strategy for developing complex and efficient optical computing systems.