bit-manipulation

Why does BinaryReader.ReadUInt32() reverse the bit pattern?

蹲街弑〆低调 提交于 2019-11-30 04:03:32
问题 I am trying to read a binary file with the BinaryReader class, and I need to read it in as blocks of UInt32, and then do some bit shifting etc. afterwords. But, for some reason bit order is reversed when I use the ReadUInt32 method. If I for example have a file where the first four bytes looks like this in hex, 0x12345678 , they end up like this after being read by ReadUInt32: 0x78563412 . If I use the ReadBytes(4) method, I get the expected array: [0x00000000] 0x12 byte [0x00000001] 0x34

Checking if bit is not set

 ̄綄美尐妖づ 提交于 2019-11-30 03:12:21
问题 If I use this: if(value & 4) to check if the bit is set, then how do I check if the bit isn't set? I tried with if(!value & 4) or if(~value & 4) and if(value ^ 4) but none of them works. 回答1: When you write if(value & 4) , C checks the result to be non-zero. Essentially, it means if((value & 4) != 0) { ... } Therefore, if you would like to check that the bit is not set, compare the result for equality to zero: if((value & 4) == 0) { ... } 回答2: You could do it many ways, but the easiest

How undefined are __builtin_ctz(0) or __builtin_clz(0)?

岁酱吖の 提交于 2019-11-30 03:11:50
Background For a long time, gcc has been providing a number of builtin bit-twiddling functions, in particular the number of trailing and leading 0-bits (also for long unsigned and long long unsigned , which have suffixes l and ll ): — Built-in Function: int __builtin_clz (unsigned int x) Returns the number of leading 0-bits in x , starting at the most significant bit position. If x is 0, the result is undefined. — Built-in Function: int __builtin_ctz (unsigned int x) Returns the number of trailing 0-bits in x , starting at the least significant bit position. If x is 0, the result is undefined.

Why were bitwise operations slightly faster than addition/subtraction operations on older microprocessors?

痴心易碎 提交于 2019-11-30 03:04:18
I came across this excerpt today: On most older microprocessors, bitwise operations are slightly faster than addition and subtraction operations and usually significantly faster than multiplication and division operations. On modern architectures, this is not the case: bitwise operations are generally the same speed as addition (though still faster than multiplication). I'm curious about why bitwise operations were slightly faster than addition/subtraction operations on older microprocessors. All I can think of that would cause the latency is that the circuits to implement addition/subtraction

Sum of two numbers with bitwise operator

♀尐吖头ヾ 提交于 2019-11-30 02:59:11
I am pasting the code to find the sum of two numbers with bitwise operator. Please suggest if it can be optimized. Thanks... public static int getSum(int p, int q) { int carry=0, result =0; for(int i=0; i<32; i++) { int n1 = (p & (1<<(i)))>>(i); //find the nth bit of p int n2 = (q & (1<<(i)))>>(i); //find the nth bit of q int s = n1 ^ n2 ^ carry; //sum of bits carry = (carry==0) ? (n1&n2): (n1 | n2); //calculate the carry for next step result = result | (s<<(i)); //calculate resultant bit } return result; } Think in entire bits: public static int getSum(int p, int q) { int result = p ^ q; // +

How can I bitwise XOR two C char arrays?

冷暖自知 提交于 2019-11-30 02:45:42
问题 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

C/C++ Code to treat a character array as a bitstream

亡梦爱人 提交于 2019-11-30 02:28:25
I have a big lump of binary data in a char[] array which I need to interpret as an array of packed 6-bit values. I could sit down and write some code to do this but I'm thinking there has to be a good extant class or function somebody has written already. What I need is something like: int get_bits(char* data, unsigned bitOffset, unsigned numBits); so I could get the 7th 6-bit character in the data by calling: const unsigned BITSIZE = 6; char ch = static_cast<char>(get_bits(data, 7 * BITSIZE, BITSIZE)); This may not work for sizes greater than 8, depending on endian system. It's basically what

How can you two-way bind a checkbox to an individual bit of a flags enumeration?

对着背影说爱祢 提交于 2019-11-30 01:52:48
For those who like a good WPF binding challenge: I have a nearly functional example of two-way binding a checkbox to an individual bit of a flags enumeration (thanks Ian Oakes, original MSDN post ). The problem though is that the binding behaves as if it is one way (UI to DataContext, not vice versa). So effectively the check box does not initialize, but if it is toggled the data source is correctly updated. Attached is the class defining some attached dependency properties to enable the bit-based binding. What I've noticed is that ValueChanged is never called, even when I force the

Bit hacking and modulo operation

£可爱£侵袭症+ 提交于 2019-11-30 01:36:57
问题 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 :

Please explain the logic behind Kernighan's bit counting algorithm

限于喜欢 提交于 2019-11-30 00:15:37
This question directly follows after reading through Bits counting algorithm (Brian Kernighan) in an integer time complexity . The Java code in question is int count_set_bits(int n) { int count = 0; while(n != 0) { n &= (n-1); count++; } } I want to understand what n &= (n-1) is achieving here ? I have seen a similar kind of construct in another nifty algorithm for detecting whether a number is a power of 2 like: if(n & (n-1) == 0) { System.out.println("The number is a power of 2"); } Stepping through the code in a debugger helped me. If you start with n = 1010101 & n-1=1010100 => 1010100 n =