bit-manipulation

Is it possible to convert from 4x Uint8 into Uint32 using typed arrays in JavaScript?

假如想象 提交于 2019-12-12 10:44:34
问题 I'm doing some bitwise manipulation in a project and I wonder if the built-in typed arrays might save me some headache and maybe even give me a bit of a performance gain. let bytes = [128, 129, 130, 131] let uint32 = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3] //=> -2138996093 Can I use typed arrays to get the same answer ? // not actually working ! let uint8bytes = Uint8Array.from(bytes) let uint32 = Uint32Array.from(uint8bytes)[0] //=> ideally i'd get the same value as

Convert 64 bits array into Int64 or ulong C#

爷,独闯天下 提交于 2019-12-12 10:37:44
问题 I have an int array of bits (length always 64) like: 1110000100000110111001000001110010011000110011111100001011100100 and I want to write it in one Int64 (or ulong?) variable. How to do it? I tried to create a BitArray and then get int , but it throws System.ArgumentException , on CopyTo line: private static Int64 GetIntFromBitArray(BitArray bitArray) { var array = new Int64[1]; bitArray.CopyTo(array, 0); return array[0]; } 回答1: That is because as mentioned in the documentation, The specified

Base-encoding efficiency using bases that are not powers of 2

北战南征 提交于 2019-12-12 10:07:48
问题 Base64 encodes three 8-bit characters onto onto four (base-64) 6-bit "characters". Base64 is efficient because it uses a base (64) and exponent (4) that happens to perfectly match a base-10 exponent of 2 (24): 3x8=4x6=24 and 2 24 =64 4 =16777216. It appears that there are no base/exponent combinations that result in values that exactly match base-10 exponents of 2 (specifically 2 n for any 0< n <256), except for base32, base64 and base128 (along with base4, base8, base16, base256, base512,

Methods to form and check bitmasks

房东的猫 提交于 2019-12-12 09:23:02
问题 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

Bitwise Operations in Ada

…衆ロ難τιáo~ 提交于 2019-12-12 09:04:23
问题 Is there a tutorial somewhere that explains on which datatypes bitwise operations can be used? I don't know why Lady Ada thinks that I cannot bitwise OR two Standard.Integer... $ gnatmake test.adb gcc -c test.adb test.adb:50:77: there is no applicable operator "Or" for type "Standard.Integer" gnatmake: "test.adb" compilation error Really? I excused the compiler for not being able to AND/OR enumerated data types. I excused the compiler for not being able to perform bitwise operations on

How to change a 32bit registers specific bits without changing other bits?

别等时光非礼了梦想. 提交于 2019-12-12 08:47:46
问题 I want to manipulate some bits of a register directly using its physical address. However I couldn't find a way to make this. I saw some posts about setting bit masks but I find them too confusing. My registers physical address is: 0x4A10005C I want to manipulate its bit which was between 18-16 bits. I want to set 0x3 inside those bits. I will be really glad if you guys can provide an answer or a way to do it. Thanks. 回答1: You can just define a pointer to the register and then use normal C

How does this color blending trick work?

為{幸葍}努か 提交于 2019-12-12 08:37:34
问题 I saw this Java code that does a perfect 50% blend between two RGB888 colors extremely efficiently: public static int blendRGB(int a, int b) { return (a + b - ((a ^ b) & 0x00010101)) >> 1; } That's apparently equivalent to extracting and averaging the channels individually. Something like this: public static int blendRGB_(int a, int b) { int aR = a >> 16; int bR = b >> 16; int aG = (a >> 8) & 0xFF; int bG = (b >> 8) & 0xFF; int aB = a & 0xFF; int bB = b & 0xFF; int cR = (aR + bR) >> 1; int cG

Packing two shorts into one int, dealing with negative and positive

↘锁芯ラ 提交于 2019-12-12 07:59:27
问题 I'm making a class PackedUnsigned1616 which stores two unsigned shorts in one int, and a class PackedSigned1616 which stores two signed shorts in one int. I've read up on bitwise operations, but I'm still confused on how to deal with signed and unsigned and values that are larger or smaller that a short's range (they are passed in as two ints). Here's what I've got so far: public final class PackedUnsigned1616 { public final int field; private static final int RIGHT = (2 << 15) - 1; private

Bitfields in C programming language

醉酒当歌 提交于 2019-12-12 06:27:30
问题 How to access entire structure members in C.Means I want to take all the data of variables in structure. struct data { char a:1; char b:2; char c:3; char d:1; } arr; I can access individual members by using . operator.But i need to access all members in that structure.Colud you please tell me how to do. 回答1: As suggested make an union of the whole data and of the bitfield structure. union { char Whole; struct data { char a:1; char b:2; char c:3; char d:1; } arr; } MyData; 来源: https:/

Dynamic Bit Shifting / Unshifting

丶灬走出姿态 提交于 2019-12-12 06:26:30
问题 i'd like to store multiple values of blocks into an integer in the same way as i can convert an ip into an int. My problem is that i need to do this for n blocks, not only 4. Also i need to specifiy the maximum value of each block, which is allways the same for all blocks. So, for the Example below, if i'd like to store the whole IP range my BlockCount is 4 and my BlockSize is 255, which is the max value of each block. But it seems not to work if i lower my BlockSize and/or BlockCount. This