#This work is licensed under the
#Creative Commons Attribution-Share Alike 3.0 United States License.
#To view a copy of this license, visit
#http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to
#Creative Commons,
#171 Second Street, Suite 300,
#San Francisco, California, 94105, USA.

INPUT "unsigned integer";
OUTPUT "Elias gamma code written to stream";
VAR value "value";
VAR egc "Elias gamma code";
if value <= 1 {
    value -> write unary 0;
} else {
    t <- floor(log 2 {value}) + 1;
    t -> write unary 0;
    value % 2 ^ {t - 1} -> write t - 1 unsigned;
}
return egc;
