bit-manipulation

Manipulate alpha bytes of Java/Android color int

二次信任 提交于 2019-12-03 04:27:02
问题 If I have an int in Java that I'm using as an Android color (for drawing on a Canvas), how do I manipulate just the alpha component of that int? For example, how can I use an operation to do this: int myOpaqueColor = 0xFFFFFF; float factor = 0; int myTransparentColor = operationThatChangesAlphaBytes(myOpaqueColor, factor); //myTransparentColor should now = 0x00FFFFFF; Ideally, it would be nice to multiply those first bytes by whatever factor is, rather than just set the bytes to a static

Optimize me! (C, performance) — followup to bit-twiddling question

谁都会走 提交于 2019-12-03 04:14:34
问题 Thanks to some very helpful stackOverflow users at Bit twiddling: which bit is set?, I have constructed my function (posted at the end of the question). Any suggestions -- even small suggestions -- would be appreciated. Hopefully it will make my code better, but at the least it should teach me something. :) Overview This function will be called at least 10 13 times, and possibly as often as 10 15 . That is, this code will run for months in all likelihood, so any performance tips would be

How to find the position of the only-set-bit in a 64-bit value using bit manipulation efficiently?

☆樱花仙子☆ 提交于 2019-12-03 04:08:15
问题 Just say I have a value of type uint64_t seen as sequence of octets (1 octet = 8-bit). The uint64_t value is known containing only one set bit at a MSB position. Thus, the uint64_t value can be in one of the following binary representations: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 10000000 pos = 7 00000000 00000000 00000000 00000000 00000000 00000000 10000000 00000000 pos = 15 00000000 00000000 00000000 00000000 00000000 10000000 00000000 00000000 pos = 23 00000000

Java integer flag and bitwise operations for memory reduction

不打扰是莪最后的温柔 提交于 2019-12-03 03:49:11
Is using an integer flag and bitwise operations an effective way of reducing the memory footprint of high volume Objects? Memory Footprint It is my understanding that commonly a boolean is stored as an int in a JVM implementation. Is this correct? In which case surely the 32 flags represent a large memory footprint reduction. Although of course the JVM implementations vary, so this may not always be the case. Performance It is my understanding that CPUs are very number driven and bitwise operations are about as efficient as things come in computing. Is there a performance penalty - or even

How to get the Nth digit of an integer with bit-wise operations?

╄→尐↘猪︶ㄣ 提交于 2019-12-03 02:40:51
问题 Example. 123456, and we want the third from the right ('4') out. The idea in practise is to access each digit seperately (ie. 6 5 4 3 2 1). C/C++/C# preferred. 回答1: A more efficient implementation might be something like this: char nthdigit(int x, int n) { while (n--) { x /= 10; } return (x % 10) + '0'; } This saves the effort of converting all digits to string format if you only want one of them. And, you don't have to allocate space for the converted string. If speed is a concern, you could

How do you randomly zero a bit in an integer?

隐身守侯 提交于 2019-12-03 01:50:32
Updated with newer answer and better test Let's say I have the number 382 which is 101111110. How could I randomly turn a bit which is not 0 to 0? The why; Since people ask me why, I simply need to do this, removing a bit from an integer. based on the answer here is the result(working one) I ran this using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Diagnostics; namespace ConsoleApplication1 { class Program { static Random random; static void Main(string[] args) { Stopwatch sw; int[] test = new int[10] { 382, 256, 1, 257, 999, 555, 412,

Microsoft Interview: transforming a matrix

依然范特西╮ 提交于 2019-12-03 01:43:49
问题 Given a matrix of size n x m filled with 0's and 1's e.g.: 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 0 if the matrix has 1 at (i,j), fill the column j and row i with 1's i.e., we get: 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 Required complexity: O(n*m) time and O(1) space NOTE: you are not allowed to store anything except '0' or '1' in the matrix entries Above is a Microsoft Interview Question. I thought for two hours now. I have some clues but can't proceed any more. Ok. The first important part

Why is squaring a number faster than multiplying two random numbers?

末鹿安然 提交于 2019-12-03 01:43:12
问题 Multiplying two binary numbers takes n^2 time, yet squaring a number can be done more efficiently somehow. (with n being the number of bits) How could that be? Or is it not possible? This is insanity! 回答1: There exist algorithms more efficient than O(N^2) to multiply two numbers (see Karatsuba, Pollard, Schönhage–Strassen, etc.) The two problems "multiply two arbitrary N-bit numbers" and "Square an arbitrary N-bit number" have the same complexity. We have 4*x*y = (x+y)^2 - (x-y)^2 So if

Bit mask in C

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 01:22:06
问题 What is the best way to construct a bit mask in C with m set bits preceded by k unset bits, and followed by n unset bits: 00..0 11..1 00..0 k m n For example, k=1, m=4, n=3 would result in the bit mask: 01111000 回答1: ~(~0 << m) << n 回答2: So, you are asking for m set bits prefixed by k reset bits and followed by n reset bits? We can ignore k since it will largely be constrained by the choice of integer type. mask = ((1 << m) - 1) << n; 回答3: I like both solutions. Here is another way that comes

Is static_cast<T>(-1) the right way to generate all-one-bits data without numeric_limits?

拜拜、爱过 提交于 2019-12-03 01:11:55
I'm writing C++ code in an environment in which I don't have access to the C++ standard library, specifically not to std::numeric_limits . Suppose I want to implement template <typename T> constexpr T all_ones( /* ... */ ) Focusing on unsigned integral types, what do I put there? Specifically, is static_cast<T>(-1) good enough? (Other types I could treat as an array of unsigned chars based on their size I guess.) Leandros Use the bitwise NOT operator ~ on 0 . T allOnes = ~(T)0; A static_cast<T>(-1) assumes two's complement, which is not portable. If you are only concerned about unsigned types,