summaryrefslogtreecommitdiffstats
path: root/host.c
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2022-02-02 22:35:13 +0100
committerAnton Luka Šijanec <anton@sijanec.eu>2022-02-02 22:35:13 +0100
commit1a22d36e7a08e9ed06ff1e8f825159e3658fe8cc (patch)
treeb1e414e71e22102ca109f71c0b62a7a1ba56ac9d /host.c
parentdebian test #2 (diff)
downloaddnsfind-1a22d36e7a08e9ed06ff1e8f825159e3658fe8cc.tar
dnsfind-1a22d36e7a08e9ed06ff1e8f825159e3658fe8cc.tar.gz
dnsfind-1a22d36e7a08e9ed06ff1e8f825159e3658fe8cc.tar.bz2
dnsfind-1a22d36e7a08e9ed06ff1e8f825159e3658fe8cc.tar.lz
dnsfind-1a22d36e7a08e9ed06ff1e8f825159e3658fe8cc.tar.xz
dnsfind-1a22d36e7a08e9ed06ff1e8f825159e3658fe8cc.tar.zst
dnsfind-1a22d36e7a08e9ed06ff1e8f825159e3658fe8cc.zip
Diffstat (limited to 'host.c')
-rw-r--r--host.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/host.c b/host.c
index 2087761..89a8524 100644
--- a/host.c
+++ b/host.c
@@ -10,8 +10,7 @@ struct in_net {
struct in_addr addr;
struct in_addr mask;
};
-/* not needed begin */
-#define POPCNT(y) int popcnt##x (uint##y##_t x) { \
+#define POPCNT(y) int popcnt##y (uint##y##_t x) { \
int c = 0; \
for (int i = 0; i < y; i++) \
if (1 << i & x) \
@@ -19,8 +18,6 @@ struct in_net {
return c; \
}
POPCNT(32)
-unsigned int power2[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648 }; /* ah yes, libmath */
-/* not needed end */
struct in_net host (struct in_net n, unsigned long long int h /* number of host in the network */) {
n.addr.s_addr = ntohl(n.addr.s_addr & n.mask.s_addr);
n.mask.s_addr = ntohl(n.mask.s_addr);
@@ -94,3 +91,15 @@ r:
*--m = o;
return r;
}
+unsigned long long int ri (unsigned long long int v, int s) { /* 000 100 010 110 001 101 011 111 */
+ int i = s-1;
+ if (!(v & 1 << (s-1)))
+ return v | 1 << (s-1);
+ while (1) {
+ if (!(v & 1 << i))
+ return v | 1 << i;
+ if (!i) /* rollover */
+ return 0;
+ v &= ~(1 << i--);
+ }
+}