Advertisement
Madmouse

binary encoding foo

Feb 9th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. // ------------------------------------------------------------------------------
  2. // THE BEER-WARE LICENSE (Revision 43):
  3. // <aaronryool@gmail.com> wrote this file. As long as you retain this notice you
  4. // can do whatever you want with this stuff. If we meet some day, and you think
  5. // this stuff is worth it, you can buy me a beer in return
  6. // ------------------------------------------------------------------------------
  7.  
  8. #define SIZE_BYTE 8
  9. #define SIZE_WORD 16
  10. #define SIZE_DOUBLEWORD 32
  11. #define SIZE_QUADWORD 64
  12.  
  13. unsigned Madnsize(long unsigned x)
  14. {
  15.     return  x&0xFFFFFFFF00000000 ? SIZE_QUADWORD :
  16.                 x&0xFFFF0000 ? SIZE_DOUBLEWORD :
  17.                     x&0xFF00 ? SIZE_WORD:
  18.                         SIZE_BYTE;
  19. }
  20.  
  21. void encode_instruction(std::vector<int>* bcode, long unsigned in)
  22. {
  23.     for(int i=(Madnsize(in)/SIZE_BYTE)-1;i>=0;i--)
  24.         bcode->push_back(0xff&((in&(0xff<<SIZE_BYTE*i))>>SIZE_BYTE*i));
  25. }
  26.  
  27.  
  28. void encode_little_endian(std::vector<int>* bcode, long unsigned in)
  29. {
  30.     for(int i=0;i<=(Madnsize(in)/SIZE_BYTE)-1;i++)
  31.         bcode->push_back(0xff&((in&(0xff<<SIZE_BYTE*i))>>SIZE_BYTE*i));
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement