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?

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

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[256] = { 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)?