bit-manipulation

Branching elimination using bitwise operators

非 Y 不嫁゛ 提交于 2019-12-04 03:10:43
I have some critical branching code inside a loop that's run about 2^26 times. Branch prediction is not optimal because m is random. How would I remove the branching, possibly using bitwise operators? bool m; unsigned int a; const unsigned int k = ...; // k >= 7 if(a == 0) a = (m ? (a+1) : (k)); else if(a == k) a = (m ? 0 : (a-1)); else a = (m ? (a+1) : (a-1)); And here is the relevant assembly generated by gcc -O3 : .cfi_startproc movl 4(%esp), %edx movb 8(%esp), %cl movl (%edx), %eax testl %eax, %eax jne L15 cmpb $1, %cl sbbl %eax, %eax andl $638, %eax incl %eax movl %eax, (%edx) ret L15:

Mysql Bitwise operations and filter

二次信任 提交于 2019-12-04 03:07:51
I try to implement a bitwise filter using MYSQL (with udf if needed) The filter is something like a AND but I want to use the mask to build a new bit string... Let me explain you with a sample : Suppose I have a table with blob storing 8 bit streams: data1: 10110110 data2: 01100010 data3: 00010011 Then I have a mask to apply to get the bits from data when mask value is 1 MASK: 00101011 And so get the following expected results: data1: 1010 data2: 1010 data3: 0011 Is there a way to optimize the filtering, without looping on each bit of "mask" to get the corresponding value in "data" row...

Creating a mask with N least significant bits set

萝らか妹 提交于 2019-12-04 03:05:20
问题 I would like to create a macro or function 1 mask(n) which given a number n returns an unsigned integer with its n least significant bits set. Although this seems like it should be a basic primitive with heavily discussed implementations which compile efficiently - this doesn't seem to be the case. Of course, various implementations may have different sizes for the primitive integral types like unsigned int , so let's assume for the sake of concreteness that we are talking returning a uint64

How to bitwise shift in VB.NET?

Deadly 提交于 2019-12-04 03:01:47
问题 How do I bitwise shift right/left in VB.NET? Does it even have operators for this, or do I have to use some utility method? 回答1: VB.NET has had bit shift operators (<< and >>) since 2003. 回答2: You can use the << and >> operators, and you have to specify how many bits to shift. myFinal = myInteger << 4 ' Shift LEFT by 4 bits. myFinal = myInteger >> 4 ' Shift RIGHT by 4 bits. You can also use it as a unary operator... myFinal <<= 4 ' Shift myFinal LEFT by 4 bits, storing the result in myFinal.

Type conversion warning after bitwise operations in C

被刻印的时光 ゝ 提交于 2019-12-04 02:51:42
How do you explain that line 7 gets a warning, but not line 5 or line 6? int main() { unsigned char a = 0xFF; unsigned char b = 0xFF; a = a | b; // 5: (no warning) a = (unsigned char)(b & 0xF); // 6: (no warning) a = a | (unsigned char)(b & 0xF); // 7: (warning) return 0; } GCC 4.6.2 output when compiled on 32-bit architecture (Windows PC): gcc -c main.c --std=c89 -Wall -Wextra -Wconversion -pedantic main.c: In function 'main': main.c:7:11: warning: conversion to 'unsigned char' from 'int' may alter its value [-Wconversion] If this helps you understand my question, here is how I see this

Number of bits to represent a number

自闭症网瘾萝莉.ら 提交于 2019-12-04 02:48:40
I'm trying to write a function to return the number of bits a positive integer less that the Javascript limit of (2^53)-1 is. However im being hit by precision problems, and want to avoid big integer libraries. Method 1: function bitSize(num) { return Math.floor( Math.log(num) / Math.log(2) ) + 1; } Pass: bitSize( Math.pow(2, 16) -1 ) = 16 Pass: bitSize( Math.pow(2, 16) ) = 17 Fail (Should be 48): bitSize( Math.pow(2, 48) -1 ) = 49 Pass: bitSize( Math.pow(2, 48) ) = 49 Method 2: function bitSize(num) { var count = 0; while(num > 0) { num = num >> 1; count++; } return count; } Pass: bitSize(

Pad a C++ structure to a power of two

限于喜欢 提交于 2019-12-04 02:31:35
I'm working on some C++ code for an embedded system. The I/O interface the code uses requires that the size of each message (in bytes) is a power of two. Right now, the code does something like this (in several places): #pragma pack(1) struct Message { struct internal_ { unsigned long member1; unsigned long member2; unsigned long member3; /* more members */ } internal; char pad[64-sizeof(internal_)]; }; #pragma pack() I'm trying to compile the code on a 64-bit Fedora for the first time, where long is 64-bits. In this case, sizeof(internal_) is greater than 64, the array size expression

Find bit position without using Log()

冷暖自知 提交于 2019-12-04 02:06:22
问题 I have an integer input that is power of 2 (1, 2, 4, 8 etc). I want the function to return bit position without using log(). For example, for inputs above will return {0, 1, 2, 3} respectively This for C#. Plus if this can be done in SQL. Thanks! 回答1: The fastest code I found to do this is from the Bit Twiddling Hacks site. Specifically, the lookup based on the DeBruijn sequence. See http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn I tested a naive method, a switch-based

Bit twiddling for checking whether a number is in particular range

只愿长相守 提交于 2019-12-04 01:50:18
I found some interesting bit twiddling in "source\common\unicode\utf.h" file of ICU library (International Components for Unicode). The bit twiddling is intended for checking whether a number is in a particular range. // Is a code point in a range of U+d800..U+dbff? #define U_IS_LEAD(c) (((c)&0xfffffc00)==0xd800) I have figured out the magic number (0xfffffc00) come from: MagicNumber = 0xffffffff - (HighBound - LowBound) However, I also found that the formula doesn't apply to every arbitrary range. Does somebody here know in what circumstance the formula works? Is there another bit twiddling

Set a specific bit in an int

大城市里の小女人 提交于 2019-12-04 01:25:44
I need to mask certain string values read from a database by setting a specific bit in an int value for each possible database value. For example, if the database returns the string "value1" then the bit in position 0 will need to be set to 1, but if the database returns "value2" then the bit in position 1 will need to be set to 1 instead. How can I ensure each bit of an int is set to 0 originally and then turn on just the specified bit? If you have an int value " intValue " and you want to set a specific bit at position " bitPosition ", do something like: intValue = intValue | (1 <<