bit-manipulation

Why does (x-1) toggle all the bits from the rightmost set bit of x?

笑着哭i 提交于 2021-02-17 05:39:08
问题 Why does this property hold true? say the kth bit from right side is the first set bit in number 'x'. (x-1) will toggle every bit upto kth bit from right side. I can verify this property by writing the bit sequence for the numbers but I don't understand as to why this property is true? Can anyone help me with a simple proof or intuition as to why this works? 回答1: Let's do a hand subtraction xxx100...00 - xxx000...01 ───────────── xxx011...11 x represents bits that we don't care Starting from

how do i rotate a value in assembly

一世执手 提交于 2021-02-16 20:40:38
问题 I am implementing a function in assembly x86 64 bits, which I am unable to alter: unsigned long rotate(unsigned long val, unsigned long num, unsigned long direction); direction- 1 is left and 0 is right. This is my code to shift left but its not working the last bit is off. Can someone help me please. rotate: push rbp push rdi push rsi push rdx mov rbp, rsp sub rsp, 16 cmp rdx, 1 je shift_left shift_left: mov rax, rdi shl rax, cl mov rax, rax mov rcx, rdi sub cl, 64 shl rcx, cl or rax rdx mov

how do i rotate a value in assembly

旧巷老猫 提交于 2021-02-16 20:40:28
问题 I am implementing a function in assembly x86 64 bits, which I am unable to alter: unsigned long rotate(unsigned long val, unsigned long num, unsigned long direction); direction- 1 is left and 0 is right. This is my code to shift left but its not working the last bit is off. Can someone help me please. rotate: push rbp push rdi push rsi push rdx mov rbp, rsp sub rsp, 16 cmp rdx, 1 je shift_left shift_left: mov rax, rdi shl rax, cl mov rax, rax mov rcx, rdi sub cl, 64 shl rcx, cl or rax rdx mov

Swapping bits at a given point between two bytes

帅比萌擦擦* 提交于 2021-02-13 17:04:12
问题 Let's say I have these two numbers: x = 0xB7 y = 0xD9 Their binary representations are: x = 1011 0111 y = 1101 1001 Now I want to crossover (GA) at a given point, say from position 4 onwards. The expected result should be: x = 1011 1001 y = 1101 0111 Bitwise, how can I achieve this? 回答1: I would just use bitwise operators: t = (x & 0x0f) x = (x & 0xf0) | (y & 0x0f) y = (y & 0xf0) | t That would work for that specific case. In order to make it more adaptable, I'd put it in a function,

Swapping bits at a given point between two bytes

元气小坏坏 提交于 2021-02-13 17:03:45
问题 Let's say I have these two numbers: x = 0xB7 y = 0xD9 Their binary representations are: x = 1011 0111 y = 1101 1001 Now I want to crossover (GA) at a given point, say from position 4 onwards. The expected result should be: x = 1011 1001 y = 1101 0111 Bitwise, how can I achieve this? 回答1: I would just use bitwise operators: t = (x & 0x0f) x = (x & 0xf0) | (y & 0x0f) y = (y & 0xf0) | t That would work for that specific case. In order to make it more adaptable, I'd put it in a function,

Swapping bits at a given point between two bytes

非 Y 不嫁゛ 提交于 2021-02-13 17:03:16
问题 Let's say I have these two numbers: x = 0xB7 y = 0xD9 Their binary representations are: x = 1011 0111 y = 1101 1001 Now I want to crossover (GA) at a given point, say from position 4 onwards. The expected result should be: x = 1011 1001 y = 1101 0111 Bitwise, how can I achieve this? 回答1: I would just use bitwise operators: t = (x & 0x0f) x = (x & 0xf0) | (y & 0x0f) y = (y & 0xf0) | t That would work for that specific case. In order to make it more adaptable, I'd put it in a function,

Is there a highly performant way to make a 56 bit integer?

删除回忆录丶 提交于 2021-02-11 14:15:10
问题 I am writing a virtual machine that has 8 bit opcodes, and one of the common types for the instructions has the 8-bit opcode, followed in memory by a 56 bit signed integer operand. Originally, I was going to implement this instruction as follows: struct machine_op { std::uint64_t opcode:8 std::int64_t operand:56; }; However, Ive heard no end of comments that using bitfields like this would not be portable, and in particular, it would not guarantee that the operand field would actually be in

Bitwise operations PHP and JS different [duplicate]

独自空忆成欢 提交于 2021-02-11 12:29:37
问题 This question already has answers here : Difference in the results of operations bitwise between javascript and php (2 answers) Closed 2 years ago . Why does php and js give different results to the same thing: JS: 1085 << 24 = 1023410176 PHP: 1085 << 24 = 18203279360 回答1: The operands of a bitwise operation in JavaScript are always treated as int32, and in PHP this is not the case. And in PHP, the bits do not get truncated because it is not treated as a 32-bit integer. Difference in the

Insert bit into uint16_t

泪湿孤枕 提交于 2021-02-11 06:18:59
问题 Is there any efficient algorithm that allows to insert bit bit to position index when working with uint16_t ? I've tried reading bit-by-bit after index , storing all such bits into array of char , changing bit at index , increasing index , and then looping again, inserting bits from array, but could be there a better way? So I know how to get, set, unset or toggle specific bit, but I suppose there could be better algorithm than processing bit-by-bit. uint16_t bit_insert(uint16_t word, int bit

Efficiently packing 10-bit data on unaligned byte boundries

*爱你&永不变心* 提交于 2021-02-10 22:23:06
问题 I'm trying to do some bit packing on multiples that don't align to byte boundries. Here's specifically what I'm trying to do. I have a 512-bit array (8 64-bit integers) of data. Inside that array is 10-bit data aligned to 2 bytes. What I need to do is strip that 512-bits down to 320-bits of just the 10-bit data (5 64-bit integers). I can think of the manual way to do that where I go through each 2-byte sections of the 512-bit array, mask out the 10-bits, or it together taking into account