bit-manipulation

uint64 UTC time

ε祈祈猫儿з 提交于 2019-12-05 03:48:42
问题 I have a UTC date time without the formatting stored in a uint64, ie: 20090520145024798 I need to get the hours, minutes, seconds and milliseconds out of this time. I can do this very easily by converting it to a string and using substring. However, this code needs to be very fast so I would like to avoid string manipulations. Is there faster way, perhaps using bit manipulation to do this? Oh by the way this needs to be done in C++ on Linux. 回答1: uint64 u = 20090520145024798; unsigned long w

Bitwise AND in Sql Server

给你一囗甜甜゛ 提交于 2019-12-05 03:41:44
I have a very typical situation. We have a table called Users which has a column called Branches (varchar 1000). The organization can have 1000 branches. So if a user has access to branch 1, 5, and 10, the branches string would look like: 1000100001000000000...... (i.e. 1 for a position a User has branch access to based on the branch's number). Please do not advise better data storage options, this is coming to me from a legacy application that is deployed across continents. Now given this background (and considering that there can be > 10000 users), I want to search for all Users who have

Optimize an array of tribools for space

谁说胖子不能爱 提交于 2019-12-05 03:39:12
Let me start with some background: By "tribool" I understand a variable which can hold one of the following values: true , false or null . In question Copying array of ints vs pointers to bools , the OP wanted to have an array of tribools (more or less) which would be as small as possible. With "a bit of" most basic bit-fu I came up a solution which used 2 bits per tribool and allowed to store the OP's array of 64 tribools in 16 bytes, which is OK. The tribool mechanics I used were simple, like: boolean A means "null or not null", boolean B means "true or false if not null". But then I thought

Why does golang RGBA.RGBA() method use | and <<?

痞子三分冷 提交于 2019-12-05 03:28:27
In the golang color package, there is a method to get r,g,b,a values from an RGBA object: func (c RGBA) RGBA() (r, g, b, a uint32) { r = uint32(c.R) r |= r << 8 g = uint32(c.G) g |= g << 8 b = uint32(c.B) b |= b << 8 a = uint32(c.A) a |= a << 8 return } If I were to implement this simple function, I would just write this func (c RGBA) RGBA() (r, g, b, a uint32) { r = uint32(c.R) g = uint32(c.G) b = uint32(c.B) a = uint32(c.A) return } What's the reason r |= r << 8 is used? From the the excellent " The Go image package " blogpost: [...] the channels have a 16-bit effective range: 100% red is

Using bitwise operators

大城市里の小女人 提交于 2019-12-05 03:24:58
问题 I've been studying C# and ran accross some familiar ground from my old work in C++. I never understood the reason for bitwise operators in a real application. I've never used them and have never had in a reason to use them. I've been studying how they work; the example below shows the shift bitwise operator. What is the point of bitwise operators, their use and how they work? Maybe I'm missing something in bitwise logic. byte bitComp = 15; // bitComp = 15 = 00001111b byte bresult = (byte)

High Order Bits - Take them and make a uint64_t into a uint8_t

北战南征 提交于 2019-12-05 02:34:47
Let's say you have a uint64_t and care only about the high order bit for each byte in your uint64_t. Like so: uint32_t: 0000 ... 1000 0000 1000 0000 1000 0000 1000 0000 ---> 0000 1111 Is there a faster way than: return ( ((x >> 56) & 128)+ ((x >> 49) & 64)+ ((x >> 42) & 32)+ ((x >> 35) & 16)+ ((x >> 28) & 8)+ ((x >> 21) & 4)+ ((x >> 14) & 2)+ ((x >> 7) & 1) ) Aka shifting x, masking, and adding the correct bit for each byte? This will compile to a lot of assembly and I'm looking for a quicker way... The machine I'm using only has up to SSE2 instructions and I failed to find helpful SIMD ops.

How to find number of 1's in a binary number in O(1) time?

女生的网名这么多〃 提交于 2019-12-05 02:01:42
问题 I know this has been asked before, but I'm looking at this particular solution listed here: int BitCount(unsigned int u) { unsigned int uCount; uCount = u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111); return ((uCount + (uCount >> 3)) & 030707070707) % 63; } How does it work? Are there any caveats involved here? Theoretically is it possible to find the answer in constant time? I mean don't we actually have to iterate through the bits to count? 回答1: Counting bits An unsigned 32-bit

Interleave bits efficiently

限于喜欢 提交于 2019-12-05 01:41:33
I need to make uint64_t out of 2 uint32_t interleaving the bits: if A=a0a1a2...a31 and B=b0b1...b31 , I need C= a0b0a1b1...a31b31 . Is there a way to do this efficiently? So far I've got only the naive approach with a for loop of 32 iterations, where each iteration does C|=((A&(1<<i))<<i)|((B&(1<<i))<<(i+1)) . I guess there should be some mathematical trick like multiplying A and B by some special number which results in interleaving their bits with zeros in the resulting 64-bit number, so that what only remains is to or these products. But I can't find such multiplier. Another potential way

How to get the bits of a “double” as a “long”

可紊 提交于 2019-12-05 01:36:16
I would like to manipulate the bitwise representation of floating-point numbers in C#. BinaryWriter and BinaryReader do it this way: public virtual unsafe void Write(double value) { ulong num = *((ulong*) &value); ... } public virtual unsafe double ReadDouble() { ... ulong num3 = ...; return *((double*) &num3); } Is there a way to do this without unsafe code, and without the overhead of actually using BinaryWriter and BinaryReader? Are you trying to avoid unsafe code altogether, or do you just want an alternative to those specific methods on BinaryReader and BinaryWriter ? You could use

Removing lowest order bit

前提是你 提交于 2019-12-05 01:33:31
问题 Given a binary number, what is the fastest way of removing the lowest order bit? 01001001010 -> 01001001000 It would be used in code to iterate over the bits of a variable. Pseudo-code follows. while(bits != 0){ index = getIndexOfLowestOrderBit(bits); doSomething(index); removeLowestOrderBit(bits); } The possible languages I'm considering using are C and Java. 回答1: Uh ... In your example, you already know the bit's index. Then it's easy: bits &= ~(1 << index); This will mask off the bit whose