bitwise

Simplest way to check if two integers have same sign?

Which is the simplest way to check if two integers have same sign? Is there any short bitwise trick to do this? ...

Most common C# bitwise operations

For the life of me, I can't remember how to set, delete, toggle or test a bit in a bitfield. Either I'm unsure or I mix them up because I rarely need these. So a "bit-cheat-sheet" would be nice to have. For example: flags = flags | FlagsEnum.Bit4; // Set bit 4. or if ((flags == FlagsEnum.Bit4)) == FlagsEnum.Bit4) // Is there a les...

Best algorithm to count the number of set bits in a 32-bit integer?

8 bits representing the number 7 look like this: 00000111 Three bits are set. What is the best algorithm to determine the number of set bits in a 32-bit integer? ...

How to handle a lot of flags for a SQL record

Hello, I need advice on how to handle relatively large set of flags in my SQL2k8 table. Two question, bear with me please :) Let's say I have 20 flags I'd like to store for one record. For example: CanRead = 0x1 CanWrite = 0x2 CanModify = 0x4 ... and so on to the final flag 2^20 Now, if i set the following combination of one recor...

How do I flip a bit in SQL Server?

I'm trying to perform a bitwise NOT in SQL Server. I'd like to do something like this: update foo set Sync = NOT @IsNew Note: I started writing this and found out the answer to my own question before I finished. I still wanted to share with the community, since this piece of documentation was lacking on MSDN (until I added it to the C...

What's the fastest way to divide an integer by 3?

int x = n / 3; // <-- make this faster // for instance int a = n * 3; // <-- normal integer multiplication int b = (n << 1) + n; // <-- potentially faster multiplication ...

2.9999999999999999 >> .5?

I heard that you could right-shift a number by .5 instead of using Math.floor(). I decided to check its limits to make sure that it was a suitable replacement, so I checked the following values and got the following results in Google Chrome: 2.5 >> .5 == 2; 2.9999 >> .5 == 2; 2.999999999999999 >> .5 == 2; // 15 9s 2.9999999999999999 >...

Is there a practical limit to the size of bit masks?

There's a common way to store multiple values in one variable, by using a bitmask. For example, if a user has read, write and execute privileges on an item, that can be converted to a single number by saying read = 4 (2^2), write = 2 (2^1), execute = 1 (2^0) and then add them together to get 7. I use this technique in several web applic...

How do I get the integer value of a char in C++?

I want to take the value stored in a 32 bit unsigned int, put it into four chars and then store the integer value of each of these chars in a string. I think the first part goes like this: char a = orig << 8; char b = orig << 8; char c = orig << 8; char d = orig << 8; ...

How to get the Nth digit of an integer with bit-wise operations?

Example. 123456, and we want the third from the right ('4') out. The idea in practise is to access each digit seperately (ie. 6 5 4 3 2 1). C/C++/C# preferred. ...

Bit Operators

If I have two things which are hex, can I someone how append their binary together to get a value? In C++, say I have unsigned char t = 0xc2; // 11000010 unsigned char q = 0xa3; // 10100011 What I want is somehow, 1100001010100011, is this possible using bit-wise operators? I want to extract the binary form of t and q and append t...

ActionScript/Flex: bitwise AND/OR over 32-bits

Question: Is there an easy way (library function) to perform a bitwise AND or OR on numbers larger than 32-bit in ActionScript? From the docs: "Bitwise operators internally manipulate floating-point numbers to change them into 32-bit integers. The exact operation performed depends on the operator, but all bitwise operations evaluate e...

Explanation of an algorithm to set, clear and test a single bit

Hey, in the Programming Pearls book, there is a source code for setting, clearing and testing a bit of the given index in an array of ints that is actually a set representation. The code is the following: #include<stdio.h> #define BITSPERWORD 32 #define SHIFT 5 #define MASK 0x1F #define N 10000000 int a[1+ N/BITSPERWORD]; void set(in...

Bonjour Networking Help on iPhone WiTap sample

I can follow most of Apple's WiTap sample, but am sort of stumped on this bit in the send method: - (void) send:(const uint8_t)message { if (_outStream && [_outStream hasSpaceAvailable]) if([_outStream write:(const uint8_t *)&message maxLength:sizeof(const uint8_t)] == -1) [self _showAlert:@"Failed sending data to peer"]; } - (voi...

How can you two-way bind a checkbox to an individual bit of a flags enumeration?

For those who like a good WPF binding challenge: I have a nearly functional example of two-way binding a checkbox to an individual bit of a flags enumeration (thanks Ian Oakes, original MSDN post). The problem though is that the binding behaves as if it is one way (UI to DataContext, not vice versa). So effectively the check box does ...

MySQL bitwise operations, bloom filter

I'd like to implement a bloom filter using MySQL (other a suggested alternative). The problem is as follows: Suppose I have a table that stores 8 bit integers, with these following values: 1: 10011010 2: 00110101 3: 10010100 4: 00100110 5: 00111011 6: 01101010 I'd like to find all results that are bitwise AND to this: 00011000 Th...

Bitwise Ops

I'm hooked on bitwise for a new security paradigm I'm creating called VMAC. Variable Matrix Access Control. I want to do logical implication on to bit strings. (1) Before I reinvent the wheel, is there an established shortcut to emulate '->' (logical implication) using AND and OR and NOT or other basic SQL binary operators? (2) XNOR ...

storing check box state in database with bitwise operation?

let say you have between 35 and 50 checkbox on a form. would you create 2 tables: accessTable, field: userid, accessType accessLookup, field: accessType, description and add X and/or remove rows to accessTable depending on the user selection OR would you create 2 tables: accessTable, field: userid, haveAccessBit accessLookup, fi...

How do I make a bitwise and in a bat-file?

I have tried with the following, but it just says "& was unexpected at this time." @echo off :enter-input echo Please enter a number between 1 and 15: echo 1 = Selection one echo 2 = Selection two echo 4 = Selection three echo 8 = Selection four echo x = Quit set INPUT= set /P INPUT=Type number: %=% if "%INPUT%" == "" goto enter-input...

Rounding off to nearest power of 2

I want to write a function that returns the nearest upper power of 2 number. For example if my input is 789, the output should be 1024. Is there any way of achieving this without using any loops but just using some bitwise operators? ...