bit-manipulation

Using logical bitshift for RGB values

两盒软妹~` 提交于 2019-12-30 06:54:22
问题 I'm a bit naive when it comes to bitwise logic and I have what is probably a simple question... basically if I have this (is ActionScript but can apply in many languages): var color:uint = myObject.color; var red:uint = color >>> 16; var green:uint = color >>> 8 & 0xFF; var blue:uint = color & 0xFF; I was wondering what exactly the `& 0xFF' is doing to green and blue. I understand what an AND operation does, but why is it needed (or a good idea) here? The source for this code was here: http:/

Mean of two ints (or longs) without overflow, truncating towards 0

﹥>﹥吖頭↗ 提交于 2019-12-30 04:33:13
问题 I'd like a way to calculate (x + y)/2 for any two integers x, y in Java. The naive way suffers from issues if x+y > Integer.MAX_VALUE, or < Integer.MIN_VALUE. Guava IntMath uses this technique: public static int mean(int x, int y) { // Efficient method for computing the arithmetic mean. // The alternative (x + y) / 2 fails for large values. // The alternative (x + y) >>> 1 fails for negative values. return (x & y) + ((x ^ y) >> 1); } ... but this rounds towards negative infinity, meaning the

What is the value of ~0 in C?

百般思念 提交于 2019-12-30 04:30:06
问题 I want to get the values of INT_MIN and INT_MAX . I've tried ~0 and ~0 >> 1 since the leftmost bit is a sign bit but I got -1 for both of them. It's so confused that why ~0 doesn't turn out to be 0xffffffff and ~0 >> 1 to be 0x7fffffff ? 回答1: Use: ~0U >> 1 Suffix 'U' for unsigned shift behavior. so, confused that why not ~0 turns out to be 0xffffffff ? See, what is 0 say in four bytes representation: BIT NUMBER 31 0 ▼ ▼ number bits 0000 0000 0000 0000 0000 0000 0000 0000 ▲ ▲ MSB LSB LSB -

Java: Are bitwise OR and AND FASTER than the equivalent logical operators?

不羁的心 提交于 2019-12-30 03:20:14
问题 Cut and dry... while I never have enough logical operations for it to be a performance bottleneck - I wonder, would I be better off using bitwise and (&) and bitwise or (|) as opposed to the same-named logical operators (&& and ||) if possible? Perhaps the question can be prefaced by the fact that I don't know of a library to convert Java to assembly to see the # of operations. 回答1: Bitwise operators avoid branching instructions, even in Java code execution. As a result you have no expensive

Why does bitwise AND of two short values result in an int value in Java?

送分小仙女□ 提交于 2019-12-29 08:47:27
问题 short permissions = 0755; short requested = 0700; short result = permissions & requested; I get a compiler error: error possible loss of precision found : int required: short If I'm not totally wrong the result of binary AND is as long as the longest operand. Why is the result an integer? Would there be a performance hit, if I would cast to short? (short) permissions & requested 回答1: If I'm not totally wrong the result of binary AND is as long as the longest operand. Why is the result an

I want to pack the bits based on arbitrary mask

二次信任 提交于 2019-12-29 07:53:51
问题 Let's say that data is 1011 1001 and the mask is 0111 0110 , then you have: input data: 1011 1001 input mask: 0111 0110 apply mask: 0011 0000 (based on `input mask`) bits selected: -011 -00- (based on `input mask`) right packed: ---0 1100 expected result: 0000 1100 (set left `8 - popcount('input mask')` bits to zero) So the final output is 0000 1100 (note that the 3 positions on the left which are unspecified are zero-filled). You can see that wherever the bits in input mask is 1 the

Calculating the negabinary representation of a given number without loops

岁酱吖の 提交于 2019-12-29 06:59:08
问题 Could you provide a convincing explanation, or a mathematical proof, to why the following function calculates the negabinary representation of a given number? function quickNegabinary(number) { var mask = 0xAAAAAAAA; return ((number + mask) ^ mask).toString(2); } 回答1: Negabinary notation Negabinary notation uses a radix of -2. This means that, as in every number system with a negative base, every other bit has a negative value: position: ... 7 6 5 4 3 2 1 0 binary: ... 128 64 32 16 8 4 2 1

packing 10 bit values into a byte stream with SIMD

给你一囗甜甜゛ 提交于 2019-12-29 04:55:07
问题 I'm trying to packing 10 bit pixels in to a continuous byte stream, using SIMD instructions. The code below does it "in principle" but the SIMD version is slower than the scalar version. The problem seem to be that I can't find good gather/scatter operations that load the register efficiently. Any suggestions for improvement? // SIMD_test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Windows.h" #include <tmmintrin.h> #include <stdint.h> #include

De Bruijn-like sequence for `2^n - 1`: how is it constructed?

那年仲夏 提交于 2019-12-29 02:51:07
问题 I'm looking at the entry Find the log base 2 of an N-bit integer in O(lg(N)) operations with multiply and lookup from Bit Twiddling hacks. I can easily see how the second algorithm in that entry works static const int MultiplyDeBruijnBitPosition2[32] = { 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9 }; r = MultiplyDeBruijnBitPosition2[(uint32_t)(v * 0x077CB531U) >> 27]; which calculates n = log2 v where v is known to be a

What does the bitwise or | operator do?

孤人 提交于 2019-12-29 01:52:26
问题 I was reading about flag enums and bitwise operators, and came across this code: enum file{ read = 1, write = 2, readandwrite = read | write } I read somewhere about why there is a inclusive or statement and how there can't be an &, but can't find the article. Can someone please refresh my memory and explain the reasoning? Also, how can I say and/or? Eg. if dropdown1="hello" and/or dropdown2="hello".... Thanks 回答1: First Question: A | does a bitwise or; a bit will be set in the result if it