bits

Macro to compute number of bits needed to store a number n

风格不统一 提交于 2020-01-13 08:13:10
问题 Let's say I need to write C macro that returns number of bits(1..32) needed to store unsigned 32-bit integer. (Result equals ceiling(log2(n)). I need it as compile-time computed macro, not a function. I could do #define NBITS(n) ((n)&(1<<31)?32:(n)&(1<<30)?31:... it works, but is rather long. (Speed does not matter here, computation is at compile time). Is there shorter way to write this macro ? Shortest ? 回答1: #define NBITS2(n) ((n&2)?1:0) #define NBITS4(n) ((n&(0xC))?(2+NBITS2(n>>2)):

Macro to compute number of bits needed to store a number n

大城市里の小女人 提交于 2020-01-13 08:13:08
问题 Let's say I need to write C macro that returns number of bits(1..32) needed to store unsigned 32-bit integer. (Result equals ceiling(log2(n)). I need it as compile-time computed macro, not a function. I could do #define NBITS(n) ((n)&(1<<31)?32:(n)&(1<<30)?31:... it works, but is rather long. (Speed does not matter here, computation is at compile time). Is there shorter way to write this macro ? Shortest ? 回答1: #define NBITS2(n) ((n&2)?1:0) #define NBITS4(n) ((n&(0xC))?(2+NBITS2(n>>2)):

It is possible to write less than 1 byte to a file

耗尽温柔 提交于 2020-01-12 13:51:23
问题 As far as I know the smallest unit in C is a byte . Where does this constraint comes from? CPU? For example, how can I write a nibble or a single bit to a file? 回答1: no, you can't... files are organized in bytes, it's the smallest piece of data you can save. And, actually, that 1 byte will occupy more than 1 byte of space, in general. Depending on the OS, the system file type, etc, everything you save as a file will use at least one block. And the block's size varies according to the file

Extract and combine bits from different bytes c c++

百般思念 提交于 2020-01-11 11:45:27
问题 I have declared an array of bytes: uint8_t memory[123]; which i have filled with: memory[0]=0xFF; memory[1]=0x00; memory[2]=0xFF; memory[3]=0x00; memory[4]=0xFF; And now i get requests from the user for specific bits. For example, i receive a request to send the bits in position 10:35, and i must return those bits combined in bytes. In that case i would need 4 bytes which contain. response[0]=0b11000000; responde[1]=0b00111111; response[2]=0b11000000; response[3]=0b00000011; //padded with

print bits of a void pointer

寵の児 提交于 2020-01-11 11:19:31
问题 If I create a void pointer, and malloc a section of memory to that void pointer, how can I print out the individual bits that I just allocated? For example: void * p; p = malloc(24); printf("0x%x\n", (int *)p); I would like the above to print the 24 bits that I just allocated. 回答1: size_t size = 24; void *p = malloc(size); for (int i = 0; i < size; i++) { printf("%02x", ((unsigned char *) p) [i]); } Of course it invokes undefined behavior (the value of an object allocated by malloc has an

print bits of a void pointer

﹥>﹥吖頭↗ 提交于 2020-01-11 11:18:15
问题 If I create a void pointer, and malloc a section of memory to that void pointer, how can I print out the individual bits that I just allocated? For example: void * p; p = malloc(24); printf("0x%x\n", (int *)p); I would like the above to print the 24 bits that I just allocated. 回答1: size_t size = 24; void *p = malloc(size); for (int i = 0; i < size; i++) { printf("%02x", ((unsigned char *) p) [i]); } Of course it invokes undefined behavior (the value of an object allocated by malloc has an

How are BigInteger stored

让人想犯罪 __ 提交于 2020-01-04 06:23:25
问题 I need to generate 512 bit BigInts, but I'm not sure which of the two below is true: 512 bits means 512 digits of 1010101010...001010 which are then converted to the decimal it represents? Or does it mean 512 digits of 0-9 , so basicly a 512-digit number with digits ranging from 0-9? Something like 12414124124....54543=512 digits. 回答1: From sourcecode, they are stored in an int array The magnitude of this BigInteger, in big-endian order: the zeroth element of this array is the most

Converting a String representation of bits to a byte

三世轮回 提交于 2020-01-01 04:41:06
问题 I'm just beginning to learn about file compression and I've run into a bit of a roadblock. I have an application that will encode a string such as "program" as a compressed binary representation "010100111111011000" (note this is still stored as a String). Encoding g 111 r 10 a 110 p 010 o 011 m 00 Now I need to write this to the file system using a FileOutputStream , the problem I'm having is, how can I convert the string "010100111111011000" to a byte[] / byte s to be written to the file

Find most significant set bit in a long

為{幸葍}努か 提交于 2019-12-31 06:41:07
问题 I'm in the unique situation where searching for "most significant bit" yields too many results and I can't find an answer that fits my needs! The question itself is pretty simple: "How do I find the most significant set bit in an unsigned long?" When I do my calculations the rightmost bit position is position '0'. I know that it involves masking the lowest bit, checking and then shifting left to once while incrementing my count, and then repeating with the 2nd lowest, etc. I've done this

High entropy random data creating functions?

ぐ巨炮叔叔 提交于 2019-12-25 08:59:22
问题 Are there functions which produce "infinite" amounts of high entropy data? Moreover, do functions exist which produce the same random data (sequentially) time after time? I kind of know that they exist, but do they have a specific name? Use case examples: Using the function to generate 100 bits of random data. (Great!) But while maintaining high values of entropy. Using the same function to generate 10000 bits of random data. (The first 100 bits generated are the same as the 100 bits of