The rapid growth of mobile computing technology and wireless communication have significantly increased the mobile users worldwide. We propose a code-based discovery protocol for cellular device-to-device (D2D) communications. To realize proximity based services such as mobile social networks and mobile marketing using D2D communications, each device should first discover nearby devices, which have mobile applications of interest, by using a discovery protocol. The proposed discovery protocol makes use of a short discovery code that contains compressed information of mobile applications in a device. A discovery code is generated by using either a hash function or a Bloom filter. When a device receives a discovery code broadcast by another device, the device can approximately find out the mobile applications in the other device. The proposed protocol is capable of quickly discovering massive number of devices while consuming a relatively small amount of radio resources. We analyze the performance of the proposed protocol under the random direction mobility model and a real mobility trace. By simulations, we show that the analytical results well match the simulation results and that the proposed protocol greatly outperforms a simple non-filtering protocol