bit-manipulation

reading binary from a file gives negative number

南楼画角 提交于 2019-12-12 02:21:55
问题 Hey everyone this may turn out to be a simple stupid question, but one that has been giving me headaches for a while now. I'm reading data from a Named Binary Tag file, and the code is working except when I try to read big-endian numbers. The code that gets an integer looks like this: long NBTTypes::getInteger(istream &in, int num_bytes, bool isBigEndian) { long result = 0; char buff[8]; //get bytes readData(in, buff, num_bytes, isBigEndian); //convert to integer cout <<"Converting bytes to

increment an infinite binary counter [closed]

本秂侑毒 提交于 2019-12-12 02:08:16
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . Taken from coding interview: How would you implement an infinite binary counter with increment in O(1) time complexity? I thought to calculate the first and second position of the rightmost 0 , but I'm not sure

How to set 45th bit in a 64 bit register field?

牧云@^-^@ 提交于 2019-12-12 01:45:12
问题 I was trying to set 46th bit in a register which of 64 bits wide using C.How do i go about setting this bit ? Currently i am doing this: uint32_t= address ; uint64_t data =1ULL << 46; Printing this is showing that bit 14 is getting set.I am not able to set even bit 32. If i set bit 32 it sets bit 0. 33 will set bit 1. Looks like it is doing circular shifting after 0-31 again it starts over with 0. Register in 64 bit wide. Any idea how do i go about setting this bit ? Eg: reg_addr.val =

Bits twiddling hack: most efficient way to remove one bit every n bits?

倾然丶 夕夏残阳落幕 提交于 2019-12-12 01:32:20
问题 Here is my question: I need to do that very efficiently (I will need to do this operation several billion times on supercomputers) in C or C++11 . N and n are known at compile-time (template parameters). What is the most efficient algorithm to do that ? Here is an example: #include <iostream> #include <climits> #include <type_traits> #include <bitset> template <unsigned int Modulo, typename Type, unsigned int Size = sizeof(Type)*CHAR_BIT, class = typename std::enable_if<std::is_integral<Type>

What is wrong with this implementation of bitwise multiplication?

回眸只為那壹抹淺笑 提交于 2019-12-11 23:45:46
问题 I am attempting to implement a method for bitwise multiplication in Galois Field 256 for the sake of building an implementation of AES. My current multiplication method is as follows: public static int multiplyPolynomials(int n, int m) { int result = 0x00000000; String ns = toBitString(n); String ms = toBitString(m); for (int i = 0; i < ns.length(); i++) { if (ns.charAt(i) == '1') { /* * If there's a 1 at place i, add the value of m left-shifted i places to the result. */ int temp = m; for

Efficient way to transpose the bit of an integer in python?

主宰稳场 提交于 2019-12-11 23:31:53
问题 Consider a 6 bits integer x = a b c d e f that should be transpose to three integers of 2 bits as follows x1 = a d x2 = b e x3 = c f What is an efficient way to do this in python? I currently goes as follows bit_list = list( bin(x)[2:] ) # to chop of '0b' # pad beginning if necessary, to make sure bit_list contains 6 bits nb_of_bit_to_pad_on_the_left = 6 - len(bit_list) for i in xrange(nb_of_bit_to_pad_on_the_left): bit_list.insert(0,'0') # transposition transpose = [ [], [], [] ] for bit in

Porting C code; need help with bitwise operation and pointer syntax

£可爱£侵袭症+ 提交于 2019-12-11 17:11:50
问题 I have some C code that I'd like to port to java. I haven't done much C coding, but I was able to follow along up until this one function. If anyone could help me understand what is going on, it would be greatly appreciated. int reverse_integer(int input) { int output = 0, i; for ( i=0, i<sizeof(int); i++ ) { output = ( input & 0x000000FF ) | output; input >>= 8; if ( i < 3 ) { output <<= 8; } } return output; } The function is used as such: char * position = //some data /*the included

Python need help understanding bit wise and byte manipulation unexpected behaviour

只愿长相守 提交于 2019-12-11 16:29:19
问题 I'm trying to build up a packet (adaption field) I'm trying to build the PCR packet, but im a bit confused on python bitwise operators.... adapt_header_3 = (0x1f45db5e4df << (9 + 6)) # adapt_header_3 = 2149055849695 # d5 7d 51 05 7f 27 - This is what the correct result should look like print(hex(adapt_header_3)) ## << but im getting this for adapt_header_3 0xfa2edaf26f8000 So I expect: d5 7d 51 05 7f 27 (6 bytes) But i'm getting: fa 2e da f2 6f 80 00 00 (8 bytes) UPDATE: Bit more of a test im

Joining hex values to create a 16bit value

柔情痞子 提交于 2019-12-11 15:32:27
问题 Im receiving three uint8 values which are the Most, Middle and Least Significant Digits of a plot value: EG: Printed in console (%c): 1 A 4 I need to pass them into a signal view UI grapher which accepts a uint16_t. So far the way im doing it is not working correctly. uint16_t iChanI = (bgp->iChanIH << 8) + (bgp->iChanIM <<4 ) + bgp->iChanIL; uint16_t iChanQ = (bgp->iChanQH << 8) + (bgp->iChanQM <<4) + bgp->iChanQL; [self updateSView:iChanI ichanQ:iChanQ]; Am i merging them correctly, or just

Flipping bits in an integer without bitwise operations

喜欢而已 提交于 2019-12-11 15:04:23
问题 I need to flip the bits in an integer from 1 to 0 and 0 to 1. E.g 10010 to 01101 The problem is that in HLSL ps_3_0 there are no binary operators. No ~, <<, >>,... Is there a mathematical way of accomplishing this? 回答1: You can use the following solution int inverse(int x) { return 0xFFFFFFFFU - x; } otherwise: int inverse(int x) { return -x - 1; // because -x = ~x + 1, only works in 2's complement } 来源: https://stackoverflow.com/questions/24107095/flipping-bits-in-an-integer-without-bitwise