jan_flanders

Untitled

Dec 19th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.61 KB | None | 0 0
  1. let rec write_bits b ~nbits x =
  2.     let x = x land ((1 lsl nbits ) - 1) in (* added *)
  3.     let n = nbits in
  4.     if n + b.nbits >= 32 then begin
  5.         if n > 31 then raise Bits_error;
  6.         let n2 = 32 - b.nbits - 1 in
  7.         let n3 = n - n2 in
  8.         write_bits b ~nbits:n2 (x asr n3);
  9.         write_bits b ~nbits:n3 (x land ((1 lsl n3) - 1));
  10.     end else begin
  11.         (* if n < 0 then raise Bits_error; *)
  12.         (* if (x < 0 || x > (1 lsl n - 1)) && n <> 31 then raise Bits_error; *)
  13.         b.bits <- (b.bits lsl n) lor x;
  14.         b.nbits <- b.nbits + n;
  15.         while b.nbits >= 8 do
  16.             b.nbits <- b.nbits - 8;
  17.             write_byte b.ch (b.bits asr b.nbits)
  18.         done
  19.     end
Advertisement
Add Comment
Please, Sign In to add comment