Is there any way I can produce sound coding it bit by bit and outputting the sound directly using a loop?
Like creating a .wav raw file but outputing the sound directly hz by hz as it loops through the code
I am using C++
Understanding the "why" will help others answer.
CAN you? Yes, you can write a CPU-based audio processor. You can use the CPU to mix the sound if you want, but then you are no longer programming a game. You are programming a digital signal processor, something your machine has many of. You are building something that the hardware was designed for, and can do both faster and better than you.
If your goal is to learn about digital signal processing, there are many excellent books on the subject. Most require some pretty intense math, but the subject is fun to many people.
If your goal is to create something useful for a game, you are probably heading down the wrong path.