bit-manipulation

C - Swap a bit between two numbers

可紊 提交于 2019-12-01 20:38:32
问题 I just tried with this code: void swapBit(unsigned char* numbA, unsigned char* numbB, short bitPosition)//bitPosition 0-x { unsigned char oneShift = 1 << bitPosition; unsigned char bitA = *numbA & oneShift; unsigned char bitB = *numbB & oneShift; if (bitA) *numbB |= bitA; else *numbB &= (~bitA ^ oneShift); if (bitB) *numbA |= bitB; else *numbA &= (~bitB ^ oneShift); } to swap bit position x of a and b but because of the if() I think there's something better. Also when i see this: *numbB &= (

Why does OR 0 round numbers in Javascript?

霸气de小男生 提交于 2019-12-01 20:28:22
I'm under the impression that the Number type in Javascript stores any number, integer or float, according to the IEEE floating point standard. If so, then why does bitwise OR-ing a number with 0 round it down? Playing around with some other bit ops, it appears that when applying bit operations to floating point numbers, the number is first rounded towards 0 and then the bit operations are applied (with the numbers in Two's complement representation rather than IEEE). Is this correct? In ECMAScript 5.1, all bitwise operations will convert the input to a 32-bit integer, and return a 32-bit

C - Swap a bit between two numbers

别说谁变了你拦得住时间么 提交于 2019-12-01 19:27:28
I just tried with this code: void swapBit(unsigned char* numbA, unsigned char* numbB, short bitPosition)//bitPosition 0-x { unsigned char oneShift = 1 << bitPosition; unsigned char bitA = *numbA & oneShift; unsigned char bitB = *numbB & oneShift; if (bitA) *numbB |= bitA; else *numbB &= (~bitA ^ oneShift); if (bitB) *numbA |= bitB; else *numbA &= (~bitB ^ oneShift); } to swap bit position x of a and b but because of the if() I think there's something better. Also when i see this: *numbB &= (~bitA ^ oneShift); I really think that there's an easier way to do it. If you have something for me, i

JavaScript Bitwise Masking

心不动则不痛 提交于 2019-12-01 19:22:59
问题 This question is similar to this other question; however, I'd like to understand why this is working as it is. The following code: console.log((parseInt('0xdeadbeef', 16) & parseInt('0x000000ff', 16)).toString(16)); console.log((parseInt('0xdeadbeef', 16) & parseInt('0x0000ff00', 16)).toString(16)); console.log((parseInt('0xdeadbeef', 16) & parseInt('0x00ff0000', 16)).toString(16)); console.log((parseInt('0xdeadbeef', 16) & parseInt('0xff000000', 16)).toString(16)); console.log((parseInt(

Bitwise Less than or Equal to

为君一笑 提交于 2019-12-01 19:01:09
There seems to be some kind of misconception that this is for a contest. I'm trying to work through an assignment and I've been stuck on this for an hour now. /* * isLessOrEqual - if x <= y then return 1, else return 0 * Example: isLessOrEqual(4,5) = 1. * Legal ops: ! ~ & ^ | + << >> * Max ops: 24 * Rating: 3 */ int isLessOrEqual(int x, int y) { int greater = (x + (~y + 1))>>31 & 1; return !(greater)|(!(x^y)); } I'm only able to use bitwise operators, as instructed in the comments. I cannot figure out how to solve x <= y ; My thought process is that I can set x as its two's complement ( ~x +1

How does this function compute the absolute value of a float through a NOT and AND operation?

佐手、 提交于 2019-12-01 18:00:26
I am trying to understand how the following code snippet works. This program uses SIMD vector instructions (Intel SSE) to calculate the absolute value of 4 floats (so, basically, a vectorized "fabs()" function). Here is the snippet: #include <iostream> #include "xmmintrin.h" template <typename T> struct alignas(16) sse_t { T data[16/sizeof(T)]; }; int main() { sse_t<float> x; x.data[0] = -4.; x.data[1] = -20.; x.data[2] = 15.; x.data[3] = -143.; __m128 a = _mm_set_ps1(-0.0); // ??? __m128 xv = _mm_load_ps(x.data); xv = _mm_andnot_ps(a,xv); // <-- Computes absolute value sse_t<float> result;

Using bitwise & operator and + in Java giving inconsistent results

↘锁芯ラ 提交于 2019-12-01 17:40:48
Could someone please explain why these two pieces of Java codes are behaving differently? First one correctly counts number of bits but the second one just displays 1 or 0 for non-zero numbers. I don't understand whats happening. public static void printNumUnitBits(int n){ int num=0; for(int i=0;i<32;i++){ int x=n&1; num=num+x; n=n>>>1; } System.out.println("Number of one bits:"+num); } public static void printNumUnitBits(int n){ int num=0; for(int i=0;i<32;i++){ num=num+n&1; n=n>>>1; } System.out.println("Number of one bits:"+num); } In Java, + has higher precedence than & . Your expression

What does the bitwise code “$n & ($n - 1)” do?

最后都变了- 提交于 2019-12-01 17:34:54
What does this code mean and what are other ways accomplish the same without using bit shifting? if ($n & ($n - 1)) Greg Hewgill That formula checks to see whether a number is a power of 2 (if your condition as written is true, then the number is not a power of two). Stated another way, your test checks to see whether there is more than one "1" bit set in the binary representation of $n . If there is zero or only one bit set, then your test will be false. It is by far the most efficient way to determine that property. First, this code is valid PHP, so your title is poor. Second, the binary

Is there a more efficient way of expanding a char to an uint64_t?

寵の児 提交于 2019-12-01 17:19:39
I want to inflate an unsigned char to an uint64_t by repeating each bit 8 times. E.g. char -> uint64_t 0x00 -> 0x00 0x01 -> 0xFF 0x02 -> 0xFF00 0x03 -> 0xFFFF 0xAA -> 0xFF00FF00FF00FF00 I currently have the following implementation, using bit shifts to test if a bit is set, to accomplish this: #include <stdint.h> #include <inttypes.h> #define BIT_SET(var, pos) ((var) & (1 << (pos))) static uint64_t inflate(unsigned char a) { uint64_t MASK = 0xFF; uint64_t result = 0; for (int i = 0; i < 8; i++) { if (BIT_SET(a, i)) result |= (MASK << (8 * i)); } return result; } However, I'm fairly new to C,

Get specific bit from uint32

[亡魂溺海] 提交于 2019-12-01 17:08:53
问题 I have a UInt32 variable like 3238844000. Now I want to get the first two bits of this number and the 6 bits after the first two bits. Both bits should be int. Decimal: 3238844000 Binary: 11000001000011001101011001100000 ^^ and Decimal: 3238844000 Binary: 11000001000011001101011001100000 ^^^^^^ 回答1: Update 2: The simplest (and also the fastest ) way for this case turns out to be by purely using the bitwise-shift operators int val = (int)(input >> 30); // performs the same int val2 = (int)(