bit-manipulation

How can I bitwise XOR two C char arrays?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 18:33:29
I feel silly for not being able to figure this out, but I am lost. I am trying to XOR two C strings. #include <stdio.h> #include <memory.h> #include <stdlib.h> int main() { char plainone[16]; char plaintwo[16]; char xor[17]; strcpy(plainone, "PlainOne"); strcpy(plaintwo, "PlainTwo"); int i=0; for(i=0; i<strlen(plainone);i++) xor[i] ^= (char)(plainone[i] ^ plaintwo[i]); printf("PlainText One: %s\nPlainText Two: %s\n\none^two: %s\n", plainone, plaintwo, xor); return 0; } My output is: $ ./a.out PlainText One: PlainOne PlainText Two: PlainTwo one^two: Why doesn't the xor array read as anything?

ARM bit field extract?

瘦欲@ 提交于 2019-11-30 18:32:16
问题 Can someone explain what this instruction does and translate it to C? ubfx.w r3, r11, #0xE, #1 According to the ARM reference manual, it does a " signed and unsigned bit field extract " but I'm not good with all that bitwise stuff. 回答1: UBFX just extracts a bitfield from the source register and puts it in the least significant bits of the destination register. The general form is: UBFX dest, src, lsb, width which in C would be: dest = (src >> lsb) & ((1 << width) - 1); The C equivalent of the

Fill with variable number of ones

此生再无相见时 提交于 2019-11-30 18:18:24
问题 What's the best way to fill a variable with an unknown (at compile time) number of ones? For example, let's say: int n = 5; int b = fillwithones(5); now b contains 11111 (in binary). I can't just hard code int b = 31 because n is not known ahead of time (in my application). I could do something like this: int b = pow(2, n) - 1 But using a pow seems very wasteful. Thanks! 回答1: You can use left shift and then subtract 1: unsigned int b = (1U << n) - 1U; // Broken down into steps // 1 =

Bit hacking and modulo operation

混江龙づ霸主 提交于 2019-11-30 17:39:43
While reading this: http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith64BitsDiv I came to the phrase: The last step, which involves modulus division by 2^10 - 1, has the effect of merging together each set of 10 bits (from positions 0-9, 10-19, 20-29, ...) in the 64-bit value. (it is about reversing the bits in a number)... so I did some calculations: reverted = (input * 0x0202020202ULL & 0x010884422010ULL) % 1023; b = 74 : 01001010 b * 0x0202020202 : 1000000010000000100000001000000010 = 9494949494 :01001010010010100100101001001010010010100 & 10884422010

C bit operations / copy one bit from one byte to another byte

拜拜、爱过 提交于 2019-11-30 17:38:00
I know how to set a bit, clear a bit , toggle a bit, and check if a bit is set. But, how I can copy bit, for example nr 7 of byte_1 to bit nr 7 in byte_2 ? It is possible without an if statement (without checking the value of the bit) ? #include <stdio.h> #include <stdint.h> int main(){ int byte_1 = 0b00001111; int byte_2 = 0b01010101; byte_2 = // what's next ? return 0; } emesx byte_2 = (byte_2 & 0b01111111) | (byte_1 & 0b10000000); You need to first read the bit from byte1 , clear the bit on byte2 and or the bit you read earlier: read_from = 3; // read bit 3 write_to = 5; // write to bit 5

Tagging/Encoding Pointers

老子叫甜甜 提交于 2019-11-30 17:20:44
问题 I need a way to tag a pointer as being either part of set x or part of set y (ie: the tag has only 2 'states'), I'm that means one can assume untagged = x and tagged = y. Currently I'm looking at using bitwise xor to do this: ptr ^ magic = encoded_ptr encoded_ptr ^ magic = ptr but I'm stumped at how to determine if the pointer is tagged in the first place. I'm using this to mark what pools nodes in a linked list come from, so that when the are delinked, they can go back to the correct perants

C++ - Bit-wise not of uchar produces int

有些话、适合烂在心里 提交于 2019-11-30 17:00:05
问题 I am surprised by C++'s behavior when applying bit-wise not to an unsigned char. Take the binary value 01010101b , which is 0x55 , or 85 . Applying bit-wise not on an eight bit representation should yield 10101010b , which is 0xAA , or 170 . However, I cannot reproduce the above in C++. The following simple assertion fails. assert(static_cast<unsigned char>(0xAAu) == ~static_cast<unsigned char>(0x55u)); I printed the values of 0x55 , 0xAA , and ~0x55 (as uchar) with the following code. And it

RGB888 to RGB565 / Bit Shifting

冷暖自知 提交于 2019-11-30 16:57:01
问题 I want to combine three characters into a short using bit shifting. This is for implementing the RGB565 color palette (where there are 5 bits for red, 6 for green, 5 for blue). Here is my example program, i'm just missing a step in the middle i think where i need to do some anding. #include <stdio.h> int main( ){ unsigned char r, g, b; unsigned short rgb; r = 255; // 0xFF 1111 1111 g = 100; // 0x64 0110 0100 b = 50; // 0x32 0011 0010 r = r >> 3; // 0x31 0001 1111 g = g >> 2; // 0x19 0001 1001

Java right shift integer by 32 [duplicate]

。_饼干妹妹 提交于 2019-11-30 16:00:30
This question already has an answer here: Findbugs warning: Integer shift by 32 — what does it mean? 2 answers I am trying to right shift an integer by 32 but the result is the same number. (e.g. 5 >> 32 is 5.) If I try to do same operation on Byte and Short it works. For example, "(byte)5 >> 8" is 0. What is wrong with Integer? JLS 15.19. Shift Operators ... If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. so shifting 32 is not effective. A Shifting conversion returns result as an int or long . So,

Compile-time recursive function to compute the next power of two of an integer?

喜欢而已 提交于 2019-11-30 15:32:55
问题 On the Bit Twiddling Hacks website the following algorithm is provided to round up an integer to the next power of two: unsigned int v; // compute the next highest power of 2 of 32-bit v v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; I would like to code a metaprogramming function that will compute the same operation: recursively (for compile-time execution) for any kind of integer (it should even work for possible awkward non-standard integers of any size like 15