bit-manipulation

Bit twiddle help: Expanding bits to follow a given bitmask

六月ゝ 毕业季﹏ 提交于 2021-01-28 04:32:28
问题 I'm interested in a fast method for "expanding bits," which can be defined as the following: Let B be a binary number with n bits, i.e. B \in {0,1}^ n Let P be the position of all 1/true bits in B , i.e. 1 << p[i] & B == 1 , and | P |= k For another given number, A \in {0,1}^ k , let Ap be the bit-expanded form of A given B , such that Ap[j] == A[j] << p[j] . The result of the "bit expansion" is Ap . A couple examples: Given B : 00 1 0 111 0, A : 0110, then Ap should be 00 0 0 110 0 Given B :

how to replicate javascript bit-shift, bit-wise operations in java,

删除回忆录丶 提交于 2021-01-27 20:54:23
问题 I am trying to replicate the behavior of javascript bit-shift and bit-wise operations in java. Did you ever try to do this before, and how can you do it reliably and consistently even with longs? var i=[some array with large integers]; for(var x=0;x<100;x++) { var a=a large integer; var z=some 'long'>2.1 billion; //EDIT: z=i[x]+=(z>>>5)^(a<<2))+((z<<4)^(a<<5)); } what would you do to put this into java? 回答1: Yes. Java has bit-wise operators and shift operators. Is there something in

Eliminating IF statement using bitwise operators

僤鯓⒐⒋嵵緔 提交于 2021-01-27 17:25:03
问题 I am trying to eliminate an IF statement whereby if I receive the number 32 I would like a '1', but any other number I would like a '0'. 32 is 0010 0000 so I thought about XOR-ing my input number with 1101 1111. Therefore if I get the number 32 I end up with 1111 1111. Now is there any way of AND-ing the individual bits (1111 1111), because if one of my XOR results is a 0, it means my final AND-ed value is 0, otherwise its a 1? EDIT: Using GCC, not Intel compiler (because I know there are a

Left shift operation on an unsigned 8 bit integer [duplicate]

ε祈祈猫儿з 提交于 2021-01-27 17:10:50
问题 This question already has answers here : what does it mean to bitwise left shift an unsigned char with 16 (2 answers) Closed 12 months ago . I am trying to understand shift operators in C/C++, but they are giving me a tough time. I have an unsigned 8-bit integer initialized to a value, for the example, say 1. uint8_t x = 1; From my understanding, it is represented in the memory like |0|0|0|0|0||0||0||1| . Now, when I am trying to left shit the variable x by 16 bit, I am hoping to get output 0

Index of single bit in long integer (in C) [duplicate]

六眼飞鱼酱① 提交于 2021-01-27 13:32:52
问题 This question already has answers here : Bit twiddling: which bit is set? (15 answers) Closed 7 years ago . I am trying to find an optimal code to locate a single bit index in a long integer (64 bit). The long integer has exactly one set bit. (Using C language) Currently, I am just shifting the whole thing by one bit, then checking for zero. I have read about lookup tables, but it won't do for the whole 64 bits. I thought about checking each 8 bits for zero, if not use a lookup, but still I

Bit shifting and bit mask - sample code

↘锁芯ラ 提交于 2021-01-27 07:33:26
问题 I've come across some code which has the bit masks 0xff and 0xff00 or in 16 bit binary form 00000000 11111111 and 11111111 00000000 . /** * Function to check if the given string is in GZIP Format. * * @param inString String to check. * @return True if GZIP Compressed otherwise false. */ public static boolean isStringCompressed(String inString) { try { byte[] bytes = inString.getBytes("ISO-8859-1"); int gzipHeader = ((int) bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00); return GZIPInputStream

How to mix two int colors correctly

随声附和 提交于 2021-01-27 05:32:11
问题 I'm trying to blend two colors that are coded as integers. Here is my little function: int blend (int a, int b, float ratio) { if (ratio > 1f) { ratio = 1f; } else if (ratio < 0f) { ratio = 0f; } float iRatio = 1.0f - ratio; int aA = (a >> 24 & 0xff); int aR = ((a & 0xff0000) >> 16); int aG = ((a & 0xff00) >> 8); int aB = (a & 0xff); int bA = (b >> 24 & 0xff); int bR = ((b & 0xff0000) >> 16); int bG = ((b & 0xff00) >> 8); int bB = (b & 0xff); int A = ((int)(aA * iRatio) + (int)(bA * ratio));

How to mix two int colors correctly

流过昼夜 提交于 2021-01-27 05:32:01
问题 I'm trying to blend two colors that are coded as integers. Here is my little function: int blend (int a, int b, float ratio) { if (ratio > 1f) { ratio = 1f; } else if (ratio < 0f) { ratio = 0f; } float iRatio = 1.0f - ratio; int aA = (a >> 24 & 0xff); int aR = ((a & 0xff0000) >> 16); int aG = ((a & 0xff00) >> 8); int aB = (a & 0xff); int bA = (b >> 24 & 0xff); int bR = ((b & 0xff0000) >> 16); int bG = ((b & 0xff00) >> 8); int bB = (b & 0xff); int A = ((int)(aA * iRatio) + (int)(bA * ratio));

How to implement arithmetic right shift in C

我怕爱的太早我们不能终老 提交于 2021-01-21 07:47:17
问题 Many lossless algorithms in signal processing require an evaluation of the expression of the form ⌊ a / 2 b ⌋, where a , b are signed ( a possibly negative, b non-negative) integers and ⌊·⌋ is the floor function. This usually leads to the following implementation. int floor_div_pow2(int numerator, int log2_denominator) { return numerator >> log2_denominator; } Unfortunately, the C standard states that the result of the >> operator is implementation-defined if the left operand has a signed

What is an XOR sum?

泪湿孤枕 提交于 2021-01-20 16:26:49
问题 I am not sure of the precise definition of this term. I know that a bitwise XOR operation is going bit by bit and taking the XOR of corresponding bits position wise. Is this result termed the 'XOR sum'? If not, what is an XOR sum, and how do you use XOR to implement this addition? 回答1: In a bit wise XOR operation: a b a^b ----------- 0 0 0 0 1 1 1 0 1 1 1 0 XOR sum refers to successive XOR operations on integers. Suppose you have numbers from 1 to N and you have to find their XOR sum then for