Set a variable to Binary?

I want to optimize a function, with a constraint that requires a variable to be 0 or 1, not other value. How do I do that?

Depending on how I do this it may converge or not.

Thanks!

Hard to respond with no code showing an example. A guess: if the variable is x then add constraints 0<=x<=1 and Element[x,Integers].