how to print memory bits in c

You would need to assign a pointer to the variable in question to a char *, and treat it as an array of bytes of length sizeof(variable). Then you can print each byte in hex using the %X format specifier to printf. You can define a function like this: void print_bytes(void *ptr, int size) { … Read more

Convert bytes to bits in python

Another way to do this is by using the bitstring module: >>> from bitstring import BitArray >>> input_str=”0xff” >>> c = BitArray(hex=input_str) >>> c.bin ‘0b11111111’ And if you need to strip the leading 0b: >>> c.bin[2:] ‘11111111’ The bitstring module isn’t a requirement, as jcollado‘s answer shows, but it has lots of performant methods for … Read more

Generate all binary strings of length n with k bits set

This method will generate all integers with exactly N ‘1’ bits. From https://graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation Compute the lexicographically next bit permutation Suppose we have a pattern of N bits set to 1 in an integer and we want the next permutation of N 1 bits in a lexicographical sense. For example, if N is 3 and the … Read more