Check ¿Por qué el género masculino suele dominar a la hora de referirse a colectivos?
Feminine is the "exclusive" gender, while masculine is the "inclusive". From RAE's guidelines
El uso genérico del masculino se basa en su condición de término no marcado en la oposición masculino/femenino. Por ello, es incorrecto emplear el femenino para aludir conjuntamente a ambos sexos, con independencia del número de individuos de cada sexo que formen parte del conjunto. Así, los alumnos es la única forma correcta de referirse a un grupo mixto, aunque el número de alumnas sea superior al de alumnos varones.
This means of course that if you say
Tengo dos hijas
It is clear that both are girls. If you had 10 children, 9 of them girls and one boy, you would still say "Tengo diez hijos". When saying
Tengo dos hijos
The ambiguity of the language doesn't give you enough information to know if both are boys if if they are one boy and one girl (and you can extend this rule to other groups, like the alumnos of the example).
To avoid the ambiguity of the language you'll need to be explicit about the gender, if you want to give that extra information to the person you are talking to:
Tengo dos hijos varones
Tengo treinta alumnos en clase, veinte de ellos alumnas.
Update:
If you want to say that you have two children and both are male (and avoid the ambiguity users point out in the comments), you could say
Tengo dos hijos, los dos varones
Tengo dos hijos, ambos varones
which conveys that the two of them are male, as opposed to only one of them (or other proportion).
Tengo treinta alumnos en clase, veinte de ellos alumnas.
Tengo treinta alumnos en clase, todos varones.
Tengo treinta alumnas en clase (this already makes clear all of them are female. No need of further disambiguation).