  In this paper, we apply two-to-one functions over $\mathbb{F}_{2^n}$ in two
generic constructions of binary linear codes. We consider two-to-one functions
in two forms: (1) generalized quadratic functions; and (2)
$\left(x^{2^t}+x\right)^e$ with $\gcd(t, n)=1$ and $\gcd\left(e,
2^n-1\right)=1$. Based on the study of the Walsh transforms of those functions
or their related-ones, we present many classes of linear codes with few nonzero
weights, including one weight, three weights, four weights and five weights.
The weight distributions of the proposed codes with one weight and with three
weights are determined. In addition, we discuss the minimum distance of the
dual of the constructed codes and show that some of them achieve the sphere
packing bound. { Moreover, several examples show that some of our codes are
optimal and some have the best known parameters.}
