The invention discloses an electromagnetic code basic unit with a self-adaptive function and a metamaterial. The metamaterial comprises N*N 2-bit programmable coding units, wherein N is a non-zero positive integer, an intelligent metamaterial unit structure comprises a metal structure layer, a surface-layer control diode, a dielectric plate layer, a total-reflection zero-transmission layer and a dielectric plate layer, all units can be used for achieving synchronous and independent real-time control by a programmable gate array, and four coding states can be generated and are corresponding tofour reflection phases of 0 degree, 90 degrees, 180 degrees and 270 degrees. The intelligent metamaterial is used for sensing position and angle of the metamaterial by a spectrometer, various types ofapplication such as beam deflection, special-mode orbital angular momentum beam and target radar cross section (RCS) reduction can be generated according to program setting in advance, so that the metamaterial is used for generating different electromagnetic responses under a special space state. The metamaterial has the advantages of simple-layer structure and the like and is easy to process.