bitmask

Fastest way to convert a integer to arbitrarily ordered byte arrays in JavaScript?

霸气de小男生 提交于 2019-12-06 19:44:37
问题 I'm looking to convert the MIN_SAFE_INTEGER through MAX_SAFE_INTEGER range of a JavaScript number (53-bits not including the sign) into a string of bits spread over 7 bytes shifted two to allow for sign and null identifiers. Thus far the best I've come up with is: function toUint8Array(data) { data = data.toString(2); data = new Array(65 - data.length).join('0') + data; var ret = new Uint8Array(data.length / 8); for (var i = 0; i < 8; i++) { ret[i] = 0; ret[i] += (data[i * 8] == '1' ? 128 : 0

How can I create a 48-bit uint for bit mask

戏子无情 提交于 2019-12-06 15:16:05
问题 I am trying to create a 48-bit integer value. I understand it may be possible to use a char array or struct, but I want to be able to do bit masking/manipulation and I'm not sure how that can be done. Currently the program uses a 16-bit uint and I need to change it to 48. It is a bytecode interpreter and I want to expand the memory addressing to 4GB. I could just use 64-bit, but that would waste a lot of space. Here is a sample of the code: unsigned int program[] = { 0x1064, 0x11C8, 0x2201,

Check if a char is ASCII using Bitmasks and Bit Operators in C

懵懂的女人 提交于 2019-12-06 03:36:18
I need to write program that will check each individual char from stdin to see if it is an ASCII character. I know that what it needs to check is if the 8th bit (7th in code, if I remember correctly) is a 0, since ASCII only uses 7 bits, but I'm having difficulty figuring out just how exactly to make it check the specific bit. This is what I have at time of writing. #include <stdio.h> #define MASK 7 int main(void) { auto char inChar; do { inChar = getchar(); // Breaks the do-while loop if it detects End of File if (inChar == EOF) { break; } printf("%c", inChar); if ( inChar == (0 & MASK)) {

What is the maximum number of categoryBitMask's allowed in Sprite Kit?

拥有回忆 提交于 2019-12-05 12:57:41
I heard only 32 different categorybitmask's are allowed to be used per Sprite Kit game. Is there any way around this? I absolutely need more then that (roughly 3-4 times more since the game is an open world one). I set up my categorybitmask's as following: static const uint64_t boundaryCategory = 0x1 << 0; static const uint64_t mainCharCategory = 0x1 << 1; ... static const uint64_t someOtherCategory = 0x1 << 31; I even changed uint32_t to uint64_t hoping that would double the amount of categorybitmask's I could use. Unfortunately, it doesn't. If anyone knows any techniques to by-pass this

How can I implement forum privileges

做~自己de王妃 提交于 2019-12-05 12:24:17
I've started developing a forum application in PHP on my MVC Framework and I've got to the stage where I assign permissions to members (for example: READ, WRITE, UPDATE, DELETE). Now, I know I can add 5 columns under the user table in my database and set them to 1 | 0, but that to me seems like too much if I want to add other rules, like MOVE for example. And how can I dynamically assign these privileges them to users individually? I've heard of using a bitmasks, but it would be really good if I could fully understand them before I continue. Do you have an example of how I might implement this

NumPy boolean array warning?

有些话、适合烂在心里 提交于 2019-12-05 02:12:21
I have a few numpy arrays, lets say a , b , and c , and have created a mask to apply to all of them. I am trying to mask them as such: a = a[mask] where mask is a bool array. It is worth noting that I have verified that len(a) = len(b) = len(c) = len(mask) And I am getting a rather cryptic sounding warning: FutureWarning: in the future, boolean array-likes will be handled as a boolean array index False == 0, and True == 1. If your mask is a list, and not an ndarray, you can get some unexpected behaviour: >>> a = np.array([1,2,3]) >>> mask_list = [True, False, True] >>> a[mask_list] __main__:1:

Bitmask: how to determine if only one bit is set

江枫思渺然 提交于 2019-12-04 23:13:24
If I have a basic bitmask... cat = 0x1; dog = 0x2; chicken = 0x4; cow = 0x8; // OMD has a chicken and a cow onTheFarm = 0x12; ...how can I check if only one animal (i.e. one bit) is set? The value of onTheFarm must be 2 n , but how can I check that programmatically (preferably in Javascript)? Ted Hopp You can count the number of bits that are set in a non-negative integer value with this code (adapted to JavaScript from this answer ): function countSetBits(i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >

How can I create a 48-bit uint for bit mask

早过忘川 提交于 2019-12-04 20:50:23
I am trying to create a 48-bit integer value. I understand it may be possible to use a char array or struct, but I want to be able to do bit masking/manipulation and I'm not sure how that can be done. Currently the program uses a 16-bit uint and I need to change it to 48. It is a bytecode interpreter and I want to expand the memory addressing to 4GB. I could just use 64-bit, but that would waste a lot of space. Here is a sample of the code: unsigned int program[] = { 0x1064, 0x11C8, 0x2201, 0x0000 }; void decode( ) { instrNum = (program[i] & 0xF000) >> 12; //the instruction reg1 = (program[i]

Methods to form and check bitmasks

亡梦爱人 提交于 2019-12-04 20:33:12
This most likely has been asked and answered before, but my searches was futile. Question is about bits, bytes masks and checking. Say one have two "triggers" 0xC4 and 0xC5 : 196: 1100 0100 0xc4 197: 1100 0101 0xc5 The simple way of checking if var is either would be: if (var == 0xc5 || var == 0xc4) { } But sometimes one see this (or the like): if ( ((var ^ magic) & mask) == 0) { } My question is how to find magic and mask . What methods, procedures, tricks etc. is to be utilized to form these values and to assert if any exists? EDIT: To clarify. Yes, in this exact example the former would be

Finding data gaps with bit masking

天涯浪子 提交于 2019-12-04 17:41:11
I'm faced with a problem of finding discontinuities (gaps) of a given length in a sequence of numbers. So, for example, given [1,2,3,7,8,9,10] and a gap of length=3 , I'll find [4,5,6] . If the gap is length=4 , I'll find nothing. The real sequence is, of course, much longer. I've seen this problem in quite a few posts, and it had various applications and possible implementations. One way I thought might work and should be relatively quick is to represent the complete set as a bit array containing 1 for available number and 0 for missing - so the above will look like [1,1,1,0,0,0,1,1,1,1] .