librlp

C library for the Recursive Length Prefix (RLP) serialization format
git clone git://git.defalsify.org/librlp.git
Info | Log | Files | Refs | README | LICENSE

bits.c (413B)


      1 
      2 int intbits_le(int len, char *n) {
      3 	char ri = 0;
      4 	char ry = 0;
      5 	char i = 0;
      6 	
      7 	if (len == 0) {
      8 		return 0;
      9 	}
     10 
     11 	for (int b = 0; b < len; b++) {
     12 		for (i = 0; i < 8; i++) {
     13 			if (((1 << i) & *(n + b)) > 0) {
     14 				ri = i + 1;
     15 				ry = b;
     16 			}			 
     17 		}
     18 	}
     19 
     20 	if (ri == 0 && ry == 0) {
     21 		ri = 1;
     22 	}
     23 
     24 	return ri + (ry * 8);
     25 }
     26 
     27 int intbytes_le(int len, char *n) {
     28 	int r = intbits_le(len, n);
     29 	return (r - 1) / 8 + 1;
     30 }