code_junkie

Is there a practical limit to the size of bit masks?

Nov 11th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. // sets the n-th bit in |bytes|. num_bytes is the number of bytes in the array
  2. // result is 0 on success, non-zero on failure (offset out-of-bounds)
  3. int set_bit(char* bytes, unsigned long num_bytes, unsigned long offset)
  4. {
  5. // make sure offset is valid
  6. if(offset < 0 || offset > (num_bytes<<3)-1) { return -1; }
  7.  
  8. //set the right bit
  9. bytes[offset >> 3] |= (1 << (offset & 0x7));
  10.  
  11. return 0; //success
  12. }
  13.  
  14. //gets the n-th bit in |bytes|. num_bytes is the number of bytes in the array
  15. // returns (-1) on error, 0 if bit is "off", positive number if "on"
  16. int get_bit(char* bytes, unsigned long num_bytes, unsigned long offset)
  17. {
  18. // make sure offset is valid
  19. if(offset < 0 || offset > (num_bytes<<3)-1) { return -1; }
  20.  
  21. //get the right bit
  22. return (bytes[offset >> 3] & (1 << (offset & 0x7));
  23. }
Add Comment
Please, Sign In to add comment