#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 bit_count,signed_;
OUTPUT "a signed value written to the stream";
VAR signed_ "signed value";
VAR bit_count "bit count";
VAR written "written value";
FUNC write_unsigned "write unsigned" "shorten:write_unsigned";
if signed_ >= 0 {
    write_unsigned(bit_count + 1, signed_ * 2);
} else {
    write_unsigned(bit_count + 1, (-signed_ - 1) * 2 + 1);
}
return written;
