bit-manipulation

Find closest integer with the same weight

Find closest integer with the same weight Question: I am trying to solve this problem: Write a program which takes as input a nonnegative integer x and returns a number y which is not equal to x, but has the same weight (same number of bits set to 1) as x and their difference is …

Total answers: 1

Does a bitwise "and" operation prepend zeros to the binary representation?

Does a bitwise "and" operation prepend zeros to the binary representation? Question: When I use bitwise and operator(&) with the number of 1 to find out if a number x is odd or even (x & 1), does the interpreter change the binary representation of 1 according to the binary representation of x? For example: …

Total answers: 1

What bytestring produces "X" on unary negation?

What bytestring produces "X" on unary negation? Question: I would like to do something like this: ~b"???" Which produces b"X". How would I find out the byte-sequence to use? Any language is fine, so tagging with python+js. Asked By: David542 || Source Answers: You can do this with XOR operations. Here’s an example in Python: …

Total answers: 1

Bitwise: Why 14 & -14 is equals to 2 and 16 & -16 is equals to 16?

Bitwise: Why 14 & -14 is equals to 2 and 16 & -16 is equals to 16? Question: it is a dummy question, but I need to understand it more deeply Asked By: Rudda Beltrao || Source Answers: A bitwise operation is a binary operation. In some representations of integers, one bit is used to …

Total answers: 2

counting 1's in a bit string to keep positions in a variable and then accessing each value

counting 1's in a bit string to keep positions in a variable and then accessing each value Question: I am finding 1’s in a bit string and then storing them. Sample Data: Sample code: def indices(chromosome): return {i for i,c in enumerate(chromosome) if c==’1′} for ind in df_initial_pop[‘initial_pop’].index: locations = indices(df_initial_pop[‘initial_pop’] [ind]) print (locations) Output: …

Total answers: 1

Bit Manipulation Hacker's Delight in Python

Bit Manipulation Hacker's Delight in Python Question: I’m trying to get into the book "Hacker’s Delight." The first formula in chapter 2 is x & (x -1) and is supposed to "turn off the rightmost 1-bit in a word producing 0 if none (e.g. 01011000 -> 0101000)." I have no idea why someone would want …

Total answers: 1

Mutation with String Encoded Chromosomes – Genetic Algorithm

Mutation with String Encoded Chromosomes – Genetic Algorithm Question: I am implementing Genetic Algorithm (GA). There are 43 numbers [Ambulance Locations] to choose from (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, …

Total answers: 1

Cross-Over with string Encoded Chromosomes

Cross-Over with string Encoded Chromosomes Question: I am implementing Genetic Algorithm (GA). There are 43 numbers [Ambulance Locations] to choose from (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, …

Total answers: 1

Getting the position of 1-bits in a python Long object

Getting the position of 1-bits in a python Long object Question: Let’s say I have a very very large python integer, in python 2.7 (though if I need to, I don’t mind switching to python 3). Something bigger than say, 2^100000. What is the fastest way by which I can find the positions of all …

Total answers: 3