We proposed and demonstrated flexible optical code label generation and processing using LiNbO <formula formulatype="inline" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink"><tex Notation="TeX">$_{3}$</tex> </formula> (LN) modulators and a variable-bandwidth spectrum shaper. First we used a transversal filter (TVF) as a decoder and performed a simulation of optical encoding and decoding to confirm basic code characteristics by taking into consideration the characteristics of O/E conversion and threshold processing. The experiments were performed using with 4 chip, 10 Gchip/s BPSK codes to verify the simulation results. We found that these codes had sufficient threshold margins for code discrimination in the simulation, and this result was in good agreement with experimental results. Then, to handle variable BPSK codes, we used LN modulators as an encoder and a TVF as a decoder. To handle more variable BPSK codes, we proposed and experimentally demonstrated optical code generation and processing using LN modulators and a variable-bandwidth spectrum shaper (VBS). This method is capable of flexible optical encoding and decoding and high-precision control in the spectral domain. We confirmed correct discrimination between matched and unmatched coded by threshold processing in a simulation and experiment using 8 chip, 20 Gchip/s codes. Finally, by encoding pulses in the Non-Return-to-Zero (NRZ) format, we increased the number of discriminable coded with 8 chip, 40 Gchip/s NRZ BPSK codes.