bitmask | Bitmask class helps you to use all might of bit masks
kandi X-RAY | bitmask Summary
kandi X-RAY | bitmask Summary
Bitmask class helps you to use all might of bit masks and don't care about what bitwise operations are. It represents a high-level abstraction that looks like simple and manageable object and behaves like collection.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Get all flags .
- Returns true if the mask contains all of the specified flags .
- Returns true if the set contains one of the given flags .
- Sets the mask .
- Adds a flag to the mask .
- Thrown when a flag is present in a mask .
- Thrown when a flag is not absent in a mask .
- Thrown when the mask is negative .
bitmask Key Features
bitmask Examples and Code Snippets
use Abibidu\Bit\Mask;
class User
{
const ROLE_ADMIN = Mask::FLAG_1;
const ROLE_MANAGER = Mask::FLAG_2;
const ROLE_CUSTOMER = Mask::FLAG_3;
/**
* @var Mask
*/
private $roles;
public function __construct()
{
Community Discussions
Trending Discussions on bitmask
QUESTION
I'm new to bit manipulation but I'm struggling to translate it to code. For the following function I need to make a bitmask
...ANSWER
Answered 2022-Apr-08 at 13:33The decimal value 5
is binary 00000101
. Shifting it left by one bit gives you 00001010
.
If you want 5
to turn into 00011111
(decimal 31) then the easiest solution is to find out the value 00100000
(decimal 32) and the subtracting one.
Remembering that we're dealing with powers of 2, if you raise 2 to the power of 5 we get 32, and can then subtract one to get the base mask. But instead of using the pow
function or multiplying in a loop, we can just shift the value 1
five steps left (1 << 5
).
Putting it all together, the you should shift 1
left by width
bits, subtract 1
, and then shift the result shift
bits to the left:
QUESTION
I have the following function, where v is a tuple of integers and hits is an integer. The function uses bitmasks to first check that no set bits are shared by any two integers in the tuple and then check that all set bits in hits are also set among the integers in the tuple
...ANSWER
Answered 2022-Mar-20 at 17:28You didn't supply any code to check if the output is correct, but I believe the following will work.
The key idea is that tot
stores all of the bits that were are set in previous numbers.
If there is any collision, then tot & v[i]
will have the repeating bit set and hence will be greater than 0.
QUESTION
I'm making a 2D scrolling shooter in Swift
with SpriteKit
. I've set up SKPhysicsBody
and using bitmasks for collisions. I keep getting intermittent errors, where the collisions will work fine and then stop working. The error I get is Fatal error: Unexpectedly found nil while unwrapping an Optional value. I don't understand why I get nil value sometimes, when it gets a value other times. I have a few different sprites in the game and after testing a lot to see if there is any difference in the collisions, I can't seem to find the problem. For example, a few play throughs and I shoot an asteroid with the laser and it will work fine. The next day the exact same thing crashes the game. Another example asteroid hits player head on and works fine, asteroid hits player from the side crashes game but next day could work fine. I don't know if the problem is with the way I've set the PhysicsBody for each sprite, as I've tried changing that and still had problems, or have I got the SKPhysicsContact
set up all wrong. Any help would be mostly appreciated, Thank you.
Striped down version of my code
...ANSWER
Answered 2022-Feb-22 at 13:43I think I fixed it. I'll post solution here incase anyone has same issue as me in the future. I had the players physicsBody like so
QUESTION
I want to load/compare/pack as runtime efficent as possible the results of 64 double comparisons into a uint64_t bitmask.
My current approach is to compare 2*2 pairs via AVX2
using _mm256_cmp_pd
. The result of both (=8) comparisons is converted into a bitmap using _mm256_movemask_pd
and via a|b<<4
assigned as byte into a union (1x uint64_t / 8 uint8_t) to save a few shifts/or's.
This example might help to visualize
...ANSWER
Answered 2022-Feb-03 at 17:08On a CPU with full-width AVX2 (like Zen2 or Haswell / Skylake), you'd probably do well with vpackssdw
/ vpacksswb
to horizontally pack down from qwords to bytes narrowing in half every time. So a total of 8 input vectors would becomes one vector that you do vpmovmskb
on (_mm256_movemask_epi8
). VCMPPD results are all-ones (-1) which stays -1, or all-zeros which stays 0, in both halves of a qword even if you use a narrower pack element size. But that packing is in-lane (within 128-bit halves of a vector), so after eventually packing down to bytes you need a vpshufb
+ vpermd
to get bytes in order before vpmovmskb
. (AMD doesn't have fast pdep
until Zen3, otherwise you could use that to interleave pairs of bits if you didn't do lane-crossing fixup shuffle.)
See How to convert 32-bit float to 8-bit signed char? (4:1 packing of int32 to int8 __m256i) for a 4:1 pack; an 8:1 makes the final shuffle more complicated unless we do more shuffles earlier, while dword chunks are small enough.
(I'm using asm mnemonic names because they're shorter to type and less clutter to read than intrinsics, and what you need to look anything up in instruction tables to find out how many uops things cost; https://uops.info/ or https://agner.org/optimize/)
But with every 256-bit SIMD operation costing 2 uops, you might do well on Zen 1 with just vmovmskpd
and scalar bit-shift / OR. If the surrounding code is all vector, having these uops use scalar integer ALUs is good. The front-end is 6 uops wide, or 5 instructions whichever is less, but there are only 4 each integer and SIMD ALU pipes, so ideally earlier and later code can overlap execution nicely. (And some specific ALU units have even more limited throughput, e.g. these shuffles on only 2 of the 4 ports.)
Or maybe one step vector packing and then _mm256_movemask_ps
? Lane-crossing shuffles are relatively expensive on Zen 1. But not too bad: vpermq
(or vpermpd
) is only 3 uops with 2 cycle throughput, vs. 2 uops with 1c throughput for vpackssdw
. (And 3 uops with 4c throughput for vpermd
.)
Assuming vpacksswd ymm
uses the same ports as the XMM version, that's FP1 / FP2. So it can partial overlap with vcmppd
which can run on FP01. (The YMM version of that also being 2 uops, 1c throughput if not mixed with other instructions.)
https://uops.info/ doesn't get that level of detail for multi-uop instructions on some AMD CPUs the way it does for Intel, but we can assume the YMM versions of non-lane-crossing versions are just two of the same uop as the XMM version where it does have that data.
You very likely don't want to use _mm256_cvtpd_ps
which costs shuffle uops and an FP->FP conversion. That costs 2 uops but only has one input vector, not two. Interpreting the compare result as a -NaN
double, you might well get a float -NaN
so it might actually work for correctness. It's definitely slower that way on most CPUs.
On Zen1 it has 2 cycle throughput, and that's per single input vector rather than a pair of vectors.
With 4x vpackssdw
we can reduce 8 vectors to 4.
Then 2x vpackssdw ymm
reduces to 2 vectors.
Then 1x vpacksswb ymm
reduces to 1 vector, with pairs of bytes in the wrong order.
For Zen 1, maybe start with 4 input vectors, and after reducing to one YMM, split it in half with vextracti128
which is only a single uop on Zen 1, for any port (since the two halves of a YMM register are already stored separately in physical registers). Then vpacksswb
the two halves together (1 uop), setting up for vpshufb xmm
(1 uop) to put pairs of bytes in the right order. That sets up for vpmovmskb
. So the only lane-crossing shuffle is just an extract.
Or instead of getting 16-bit chunks of bitmap, you could maybe do the above twice, then vinserti128 ymm, xmm, 1
(2 uops, 0.67c throughput) / vpmovmskb ymm
(1 uop) to get a 32-bit chunk of bitmap. Those 3 uops replace 2x vpmovmskb xmm
/ shl
/ or
, so you're saving a uop, and have good flexibility of what vector ALU port they can run on. Although it is more vector ALU pressure.
QUESTION
I am trying to install some logic into a piece of JS I am working on that will simply add/remove flags depending on some conditions.
It primarily consists of two steps
- Build object based on some stateful data
- Hand off to post initialization method to parse data array of objects
In Step 1
, there is a 'status' property, which can have a number of flags:
ANSWER
Answered 2022-Feb-02 at 16:30You can't use bitwise operations like this with values that are not powers of 2.
The value 3 could be any of the follwing values:
CURRENT
COMPLETE | INCOMPLETE
CURRENT | COMPLETE
CURRENT | INCOMPLETE
CURRENT | COMPLETE | INCOMPLETE
There's no way to tell which combination it is.
Change the values to
- COMPLETE = 1
- COMPLETE = 2
- CURRENT = 4
- NOT_APPLICABLE = 8
and it's so easy to do bitwise operations with powers of 2.
QUESTION
Given are 2 bitmasks, that should be accessed alternating (0,1,0,1...). I try to get a runtime efficient solution, but find no better way then following example.
...ANSWER
Answered 2022-Jan-28 at 02:44This is quite hard to optimize this loop. The main issue is that each iteration of the loop is dependent of the previous one and even instructions in the loops are dependent. This creates a long nearly sequential chain of instruction to be executed. As a result the processor cannot execute this efficiently. In addition, some instructions in this chain have a quite high latency: tzcnt
has a 3-cycle latency on Intel processors and L1 load/store have a 3 cycle latency.
One solution is work directly with registers instead of an array with indirect accesses so to reduce the length of the chain and especially instruction with the highest latency. This can be done by unrolling the loop twice and splitting the problem in two different ones:
QUESTION
I have trouble to grasp how to use colors in CGA/EGA/VGA video graphics modes. The video modes I'm particularly interested in are 0Dh (EGA 320x200) and 12h (VGA 640x480). Both of these modes have 4 planes, thus 16 colors.
My (probably incorrect) understanding is that I should activate a set of planes by writing a bitmask to port 03C4h
, then when I write to video memory, the data only gets written to the activated planes. Mostly I used this document to get my information, though I also encountered several other tutorials and discussions:
http://www.techhelpmanual.com/89-video_memory_layouts.html
Now I'm trying to write pixels in all possible colors in the first word in the video memory (top left part of screen). I load 1 for the initial bitmask to AH and 1 bit to BX. Then in a loop, I increment AH and shift (SHL
) the bit in BX to hit a different pixel next time. I OR
BX to A000h:0000h
to add each pixels by leaving the already existing pixels untouched.
What I'm expected to see is a line of pixels in all possible 16 EGA colors on the top left of the screen. What I actually see is 7 white and 1 bright yellow dots with black pixels in between them. What am I doing wrong?
Also, every tutorial says that I must write 0005h
to port 03CEh
before I start to use planes. What is the purpose of that? When I comment those lines out, I can still use planes (I mean, in other programs). Previously I had success using planes when I was writing to different words in video memory (so I didn't need different color pixels in one block of 16 pixels that's represented by a single word in video memory); and when I used BIOS functions (e.g. INT 10h/AH=0Ch) to write pixels, but still I want to understand how to use planar graphics without BIOS, as I believe the BIOS functions are slow.
Here is my code (indentation is optimized for 8-width tabs, so it kind of looks off here):
...ANSWER
Answered 2022-Jan-17 at 01:56Writing the word 0005h to ports 03CEh and 03CFh will select write mode 0. This is a complex mode that involves many features of the VGA but luckily for us most of these are reset when the video mode is set.
However your code still needs to do the following:
- In order to fill the VGA's internal 32-bit latch, you must perform a read-before-write operation
- Restricting output to a single or a few pixels is done using the BitMask register.
Next snippet displays a rainbow of 16 vertical lines that are 1 pixel wide:
QUESTION
We were able to have custom raycasting using bitmasks:
...ANSWER
Answered 2021-Dec-19 at 11:18In SceneKit you can use bitmasks in context of [SCNHitTestResult]. hitTest(_:options:)
instance method is not deprecated yet and it works in iOS 15.2.
QUESTION
In matplotlib I show an image using matplotlib.pyplot.imshow
. I also know how to draw a filled Polygon. One of the required arguments is xy
a numpy array of shape (N, 2) where each of the N rows represents the x-y co-ordinates of a polygon point.
Now, a filled polygon rendered onto an image can also be represented by a bitmask (with the same height and width as the image), with 1s for fill, and 0s otherwise.
My question is, if I have a bitmask rather than polygon points, is there a way to draw a filled Polygon in matplotlib directly from that? And I mean in the general case where the bitmask may have more than one contiguous blob, or it may have holes:
It doesn't have to be a Polygon, but it should still be an Artist of some sort.
Note: There is this similar question Create matplotlib polygon patch using image mask but it has not been satisfactorily answered. Finding contours is an idea, but I want to know if there is something more direct in matplotlib.
...ANSWER
Answered 2021-Dec-18 at 12:44You can overlay the bitmask as a another AxesImage
over your image by converting the bitmask into an RGBA image (overlay
in the example):
QUESTION
For sake of experiment lets have a function that takes in a bitmask and offset and returns the mask shifted by offset. What would be a performance friendly way to determine if the operation will not shift any parts of the bitmask past the width of the data type? This is what I've tried so far, but maybe there is more optimal way to check this?
Example program: (Note: I'm looking for a solution for all data types, not only 16 bit integers)
...ANSWER
Answered 2021-Oct-19 at 14:24Not sure would this be faster, but you can check whether before and end value have same number of bits set
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install bitmask
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page