Revert example code update

This commit is contained in:
Jon Staab 2024-09-09 12:38:51 -07:00 committed by fiatjaf_
parent 7edd3d23a9
commit e7eb776288

47
13.md
View File

@ -48,37 +48,30 @@ Validating
Here is some reference C code for calculating the difficulty (aka number of leading zero bits) in a nostr event id: Here is some reference C code for calculating the difficulty (aka number of leading zero bits) in a nostr event id:
```c ```c
#include <stdio.h> int zero_bits(unsigned char b)
#include <stdlib.h> {
#include <string.h> int n = 0;
int countLeadingZeroes(const char *hex) { if (b == 0)
int count = 0; return 8;
for (int i = 0; i < strlen(hex); i++) { while (b >>= 1)
int nibble = (int)strtol((char[]){hex[i], '\0'}, NULL, 16); n++;
if (nibble == 0) {
count += 4; return 7-n;
} else { }
count += __builtin_clz(nibble) - 28;
/* find the number of leading zero bits in a hash */
int count_leading_zero_bits(unsigned char *hash)
{
int bits, total, i;
for (i = 0, total = 0; i < 32; i++) {
bits = zero_bits(hash[i]);
total += bits;
if (bits != 8)
break; break;
} }
} return total;
return count;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <hex_string>\n", argv[0]);
return 1;
}
const char *hex_string = argv[1];
int result = countLeadingZeroes(hex_string);
printf("Leading zeroes in hex string %s: %d\n", hex_string, result);
return 0;
} }
``` ```