Advertisement
Guest User

Untitled

a guest
Jul 15th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include <bitset>
  2. #include <iostream>
  3. #include <array>
  4.  
  5. using u32 = unsigned int;
  6. std::array<u32, 1024> data{};
  7.  
  8. u32 bit_buf = 0;
  9. u32 bit_pos = 0;
  10. u32 write_ptr = 0;
  11.  
  12. void put_bit(u32 bit) {
  13. if (bit_pos > 32) {
  14. data[write_ptr++] = bit_buf;
  15. bit_buf = 0;
  16. }
  17. bit_buf |= (bit << bit_pos);
  18. bit_pos++;
  19. }
  20.  
  21. void put_byte(int byte) {
  22. if (bit_pos > 24) {
  23. const u32 num_bits = 32 - bit_pos;
  24. const u32 mask = (1 << num_bits) - 1;
  25. bit_buf |= ((byte & mask) << bit_pos);
  26. data[write_ptr++] = bit_buf;
  27. byte >>= num_bits;
  28. bit_buf = 0;
  29. }
  30. bit_buf |= (byte << bit_pos);
  31. bit_pos++;
  32. }
  33.  
  34. void put_dword(u32 dword) {
  35. if (bit_pos > 0) {
  36. const u32 num_bits = 32 - bit_pos;
  37. const u32 mask = (1 << num_bits) - 1;
  38. bit_buf |= ((dword & mask) << bit_pos);
  39. data[write_ptr++] = bit_buf;
  40. dword >>= num_bits;
  41. bit_buf = 0;
  42. }
  43. bit_buf |= (dword << bit_pos);
  44. bit_pos++;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement