bit-manipulation

C macro to get the smallest power of two greater than a given number

心已入冬 提交于 2019-12-24 11:01:13
问题 I need a C macro to get the smallest of power two greater than a given number. For example, FIRSTFREEBIT(0x16) (binary 1_0110 ) must be equal to 0x20 . I am going to use it as: #include <someheader.h> // defines SOME_X and SOME_Y enum { x = SOME_X, y = SOME_Y, z = FIRSTFREEBIT(x|y), t = z << 1, }; A similar, but slightly different SO question: Algorithm for finding the smallest power of two that's greater or equal to a given value 回答1: Here's my code, you are welcome to invent something

Checking specific bits of a bitmask

一笑奈何 提交于 2019-12-24 10:23:03
问题 I am working with Bitmasks in python . As far as I know, these are arrays of integers that when they are unpacked into binary format they tell you which of the 32 bits are set (=1) for a given element in the array. I would like to know the fastest way to check whether 4 specific bits are set or not for any element of an array. I do not care about the rest. I have tried the following solution but it is not fast enough for my purpose: def detect(bitmask, check=(18,22,23,24), bits=32): boolmask

C++ bitwise operations

天大地大妈咪最大 提交于 2019-12-24 08:49:26
问题 I am operating on individual bits of two integers, (i am using g++ for compilation on Ubuntu machine). In some intermediate step, I have the bit representations as q = 11000000000000000000000000000000 q_1 = 00000000000000000000000000000001 Now I want to check whether unit's places of q and q_1 are both same or not. so, I am checking (*q)&1==q_1 in the if condition, and its working fine. But whenever I want to check that unit's place of q is 0 and that of q_1 is 1, I thought I should do ((*q)

Binary print not working in C

馋奶兔 提交于 2019-12-24 08:34:44
问题 I'm trying to print binary using bit mask of 32 bit in c but the binary representation is not getting printed in the if statement. unsigned int bit_mask = 2147483648; int decimal = 2; printf("\nBinary representation of 2: \n"); while(bit_mask > 0){ if((decimal & bit_mask) == 0) printf("0"); else printf("1"); bit_mask = bit_mask >> 1; } decimal = 255; printf("\n\nBinary representation of 255: \n"); while(bit_mask > 0){ if((decimal & bit_mask) == 0) printf("0"); else printf("1"); bit_mask = bit

Simple XOR ruby 1.9.2

人盡茶涼 提交于 2019-12-24 07:05:32
问题 Apparently this used to work on ruby 1.8.7 but unfortunately not on 1.9.2 class String def xor(key) text = dup text.length.times {|n| text[n] ^= key[n.modulo key.size] } text end end def encode(_original, _pass = 'testvendor') _original.xor(_pass) end puts encode('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

GCC Bit-scan-forward to find next set bit?

笑着哭i 提交于 2019-12-24 05:47:15
问题 I have a uint64_t and I would like to find the index of the first set bit, reset it to zero and find the next set bit. How do I know when to terminate? BSF on all zeros is undefined... const uint64_t input = source; if(0 != input){ int32_t setIndex = GCC_BSF_INTRINSIC(input); while(setIndex != UNDEFINED???){ //Do my logic //Reset input[setIndex] = 0; setIndex = BSF_Variant(input); } } Could somebody please help? 回答1: The simplest would be to just check the input: while (input) { int32_t index

How do bit fields interplay with bits padding in C

北城余情 提交于 2019-12-24 04:09:39
问题 I have two questions concerning bit fields when there are padding bits. Say I have a struct defined as struct T { unsigned int x: 1; unsigned int y: 1; }; Struct T only has two bits actually used. Question 1: are these two bits always the least significant bits of the underlying unsigned int? Or it is platform dependent? Question 2: Are those unused 30 bits always initialized to 0? What does the C standard say about it? 回答1: Question 1: are these two bits always the least significant bits of

bit shift multiplication in c not using powers of 2 [duplicate]

China☆狼群 提交于 2019-12-24 03:51:40
问题 This question already has answers here : How can I multiply and divide using only bit shifting and adding? (13 answers) Closed 6 years ago . How can I perform multiplication by 36 using bit-shifting? Isn't it only possible to multiply by powers of 2? For example: unsigned x = 4; // binary 00000000 00000000 00000000 00001000 unsigned y = x << 3; // multiply by 8, resulting in binary 00000000 ... 00100000 Thanks! 回答1: You can't multiply by a non-power of 2 by bit shifting alone. But you can

Rational comparison of bits

佐手、 提交于 2019-12-24 03:26:13
问题 I have a number of type int. It lies within [0,255]. That is, includes 8 bits. I need to check often say: 2(int) = 00000010(Binary) 1. The bit 6 and bit 7 must be equal to 0 and 1 respectively. And I check it like this: if ((!(informationOctet_ & (1 << 6))) && (informationOctet_ & (1 << 7))) { ... } But it is not very readable, whether it is possible - to do something "beautiful"? I can not use the std::bitset, my head says it's a waste of resources and you can not do without it. 回答1: There

How to add and subtract 16 bit floating point half precision numbers?

爱⌒轻易说出口 提交于 2019-12-24 03:15:10
问题 How do I add and subtract 16 bit floating point half precision numbers? Say I need to add or subtract: 1 10000 0000000000 1 01111 1111100000 2’s complement form. 回答1: Assuming you are using a denormalized representation similar to that of IEEE single/double precision, just compute the sign = (-1)^S, the mantissa as 1.M if E != 0 and 0.M if E == 0, and the exponent = E - 2^(n-1), operate on these natural representations, and convert back to the 16-bit format. sign1 = -1 mantissa1 = 1.0