# How can I flip n number of bits in a number in assembly?

How can I flip "n" number of lower/higher bits in a number using the xor operation in assembly? How would the mask be calculated? For example, if n=2 for "1110" the mask would be "1100" for higher 2 and "0011" for lower two, however for "111111" it would be "110000" for higher two, and "000011"… Read More How can I flip n number of bits in a number in assembly?

# Have some doubts in the operation [Bitwise Manipulation]

#include <iostream> #include <math.h> using namespace std; // converting from decimal to binary int main() { int n, bit, ans = 0, i = 0; cout << "enter a number: "; cin >> n; while (n > 0) { bit = n & 1; ans = round(bit * pow(10, i)) + ans; /* we used… Read More Have some doubts in the operation [Bitwise Manipulation]

# How can I toggle certain bits in an 8bit register?

Say I have bits 0-3 I want to toggle given a certain register value, how can I do that? eg: unsigned char regVal = 0xB5; //1011 0101 // Toggle bits 0-3 (0101) to 1 without changing 4-7 where result should be 1011 1111 unsigned char result = regVal & (0x01 | 0x02 | 0x03 |… Read More How can I toggle certain bits in an 8bit register?

# recombining 32 bit integer from 8 bit components

I have read 4 chars from a file. For instance 11110000 00001111 11000011 00111100 read in that order. i need to combine these from individual chars to form a continuous single uint32_t 11110000000011111100001100111100. Here is a solution i decided that seemed to work until it didn’t. //#include <fstream> #include <cstdint> #include <iostream> //std::fstream File("example", std::ios::binary… Read More recombining 32 bit integer from 8 bit components

# Inconsistency between C# and Javascript bitwise operator

Given the following code: uint HASHTABLE = 1917507215; for (uint i = 5; i != 0; i–) { HASHTABLE = (HASHTABLE ^ ((HASHTABLE << 10 ^ HASHTABLE) << 9 ^ HASHTABLE) << 5) & 0x7fffffff ^ (((HASHTABLE << 7 ^ HASHTABLE) << 10 ^ HASHTABLE) << 9 ^ HASHTABLE) << 5; HASHTABLE = HASHTABLE >>… Read More Inconsistency between C# and Javascript bitwise operator

# Can anyone explain to me what this shifting means under this circumstance: uint16_t register0 = (instruction >> 9) & 0x7

I’m trying to write a VM (LC-3), and on this ADD instruction I encountered this statement. Basically the "register0" is the DR register, but I don’t really understand what is actually shifting and why 9. Also the AND operator with the 0x7 value. |15|14|13|12|11|10|9|8|7|6|5|4|3|2|1|0| | 0001 | DR | SR1 |0| 00| SR2 | Could… Read More Can anyone explain to me what this shifting means under this circumstance: uint16_t register0 = (instruction >> 9) & 0x7

# Incorrect bitwise result

I’m working on a C project for a PIC microcontroller in MikroC and have a function: void Write_SPI_32(unsigned long Address, unsigned long Data); That only sometimes works: Write_SPI_32(0x300000 + 12, 0b10001111000010100000000000000000); //Gives expected result Write_SPI_32(0x300000 + 12, 0x8f0a0000); //Gives expected result Write_SPI_32(0x300000 + 12, 2399797248); //Gives expected result Write_SPI_32(0x300000 + 12, (2 << 30) |… Read More Incorrect bitwise result

# How to generate lookup table for counting leading zeroes (clzlut)?

I found this function, but there is no explanation of where the clzlut lookup table came from (and I searched for many hours for it on the web, couldn’t find anything): static uint8_t clzlut = { 8,7,6,6,5,5,5,5, 4,4,4,4,4,4,4,4, 3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,… Read More How to generate lookup table for counting leading zeroes (clzlut)?

# Convert a numpy bool array to int

I have a numpy array (dtype bool) representing an array of bits. For example, the array np.array([True, False, False], dtype=bool) represents the number 4 (indeed, bin(4) == 0b100). I would like to convert the numpy array to an integer (4 in the previous example). So far I’ve tried with an iterative approach: bits = np.array([True,… Read More Convert a numpy bool array to int