1 // Copyright 2016 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "net/tools/huffman_trie/bit_writer.h" 6 7 #include "base/check.h" 8 9 namespace net::huffman_trie { 10 11 BitWriter::BitWriter() = default; 12 13 BitWriter::~BitWriter() = default; 14 WriteBits(uint32_t bits,uint8_t number_of_bits)15void BitWriter::WriteBits(uint32_t bits, uint8_t number_of_bits) { 16 DCHECK(number_of_bits <= 32); 17 for (uint8_t i = 1; i <= number_of_bits; i++) { 18 uint8_t bit = 1 & (bits >> (number_of_bits - i)); 19 WriteBit(bit); 20 } 21 } 22 WriteBit(uint8_t bit)23void BitWriter::WriteBit(uint8_t bit) { 24 current_byte_ |= bit << (7 - used_); 25 used_++; 26 position_++; 27 28 if (used_ == 8) { 29 Flush(); 30 } 31 } 32 Flush()33void BitWriter::Flush() { 34 position_ += (8 - used_); 35 bytes_.push_back(current_byte_); 36 37 used_ = 0; 38 current_byte_ = 0; 39 } 40 41 } // namespace net::huffman_trie 42