Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
/* Buffer is big-endian */
/* Get bits in order of descending significance */
uint8_t get_bit(const uint8_t *x, size_t n)
{
return (x[n / 8] & (1 << (7 - (n % 8)))) != 0;
}
void test_get_bit(void)
{
uint8_t zero_sum = 0;
uint8_t one_sum = 0;
uint8_t b[2] = {0b10110001, 0b10100110};
zero_sum = get_bit(b, 1) + get_bit(b, 4) + get_bit(b, 5) + get_bit(b, 6)
+ get_bit(b, 9) + get_bit(b, 11) + get_bit(b, 12) + get_bit(b, 15);
one_sum = get_bit(b, 0) + get_bit(b, 2) + get_bit(b, 3) + get_bit(b, 7)
+ get_bit(b, 8) + get_bit(b, 10) + get_bit(b, 13) + get_bit(b, 14);
if (zero_sum == 0 && one_sum == 8)
puts("Test: get_bit passed!");
else
puts("Test: get_bit FAILED!");
}
/* Compute (2 * buffer) */
uint8_t bitshift(uint8_t *buffer, size_t n, uint8_t next_bit)
{
size_t i;
uint8_t carry;
carry = get_bit(buffer, 0);
for (i = 0; i < n - 1; ++i) {
buffer[i] <<= 1;
buffer[i] |= get_bit(buffer, 8 * (i + 1));
}
buffer[i] <<= 1;
buffer[i] |= (next_bit && 1);
return carry;
}
void test_bitshift(void)
{
uint8_t b[2] = {0b10110001, 0b10100110};
uint8_t t[2] = {0b11000110, 0b10011001};
uint8_t carry1, carry2;
carry1 = bitshift(b, 2, 0);
carry2 = bitshift(b, 2, 1);
if (carry1 == 1 && carry2 == 0 && b[0] == t[0] && b[1] == t[1])
puts("Test: bitshift passed!");
else
puts("Test: bitshift FAILED!");
}
/* Compute (2^n - buffer) */
void complement(uint8_t *buffer, size_t n)
{
size_t i;
int found_one = 0;
for (i = 8 * n - 1; i != -1; --i) {
if (found_one)
buffer[i / 8] ^= 1 << (7 - (i % 8));
else if (get_bit(buffer, i))
found_one = 1;
}
}
void test_complement(void)
{
uint8_t b[2] = {0b10110001, 0b10100000};
uint8_t t[2] = {0b01001110, 0b01100000};
complement(b, 2);
if (b[0] == t[0] && b[1] == t[1])
puts("Test: complement passed!");
else
puts("Test: complement FAILED!");
}
/* Tent map with mu=2 over (0, 2^64) */
uint8_t tent(uint8_t *buffer, size_t n, uint8_t next_bit)
{
if (get_bit(buffer, 0)) {
complement(buffer, n);
bitshift(buffer, n, next_bit);
return 1;
} else {
bitshift(buffer, n, next_bit);
return 0;
}
}
void run_a_bit(void)
{
uint8_t buffer[] = {0x19, 0xb3, 0x9f, 0x77};
uint8_t out;
int iterations = 0;
while (buffer[0] || buffer[1] || buffer[2] || buffer[3]) {
out |= tent(buffer, 4, 1);
++iterations;
if (iterations % 8 == 0) {
printf("%d, ", out);
iterations = 0;
out = 0;
} else {
out <<= 1;
}
}
}
void analyse(void)
{
long int table[256] = {0};
uint8_t buffer[] = {
90, 87, 57, 41, 140, 213, 23, 230, 5, 220, 177, 171, 99, 67, 136, 96,
215, 151, 235, 92, 83, 53, 234, 112, 164, 200, 106, 58, 211, 10, 180,
230
};
uint8_t out;
int i;
for (i = 0; i < 100000000; ++i) {
out |= tent(buffer, 32, 1 && (i % 7));
if (i % 8 == 0) {
table[out]++;
out = 0;
} else {
out <<= 1;
}
}
for (i = 0; i < 256; ++i) {
printf("%d:\t%d\n", i, table[i]);
}
}
int main(void)
{
test_get_bit();
test_bitshift();
test_complement();
analyse();
/* const size_t bufferlen = 4;
uint8_t buffer[2] = {0U, 255U};
printf("%" PRIu8 ",%" PRIu8 "\n", buffer[0], buffer[1]);
bitshift(buffer, bufferlen, 0);
printf("%" PRIu8 ",%" PRIu8 "\n", buffer[0], buffer[1]);
bitshift(buffer, bufferlen, 1);
printf("%" PRIu8 ",%" PRIu8 "\n", buffer[0], buffer[1]);
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment