background preloader

Bitwise Logic

Facebook Twitter

Using Bit-wise Math to Simplify Logic - Strictly speaking, bit strings are not nearly as easy to understand as named boolean variables; but there are situations when they can simplify or eliminate the need for long boolean expressions.

Using Bit-wise Math to Simplify Logic -

Consider my recent post about the ShortcutManager. There are three control keys that may or may not be pressed at any given time. This amounts to 8 possible key combinations (7 if you ignore when none of them are pressed). Here’s what this would look like if you used a boolean expression to check for each of the possible combinations: That might not seem too bad, and it isn’t that hard to understand what’s going on (even without the comments). With a simple method to construct my “bit string”, we can greatly simplify this. Having this integer allows me to simplify the logic as follows (which isn’t harder to understand at all, if you use comments): This is powerful, but it’s just scratching the surface. Tutorials - Bitwise Operators and Bit Manipulations in C and C.

Generally, as a programmer you don't need to concern yourself about operations at the bit level.

Tutorials - Bitwise Operators and Bit Manipulations in C and C

You're free to think in bytes, or ints and doubles, or even higher level data types composed of a combination of these. But there are times when you'd like to be able to go to the level of an individual bit. BlackWasp Software Development. In the previous part of the C# Fundamentals tutorial I described the Boolean operators and their effect on Boolean values.

BlackWasp Software Development

The bitwise logic operators provide the same logical AND, OR and XOR functions for operation on each bit of integer values. This can be very useful for interrogating the value of individual bits in integers used as bit fields. For developers who are new to interrogating individual bits in bit fields, consider the following example. Bitwise Math - ITPedia.

From ITPedia What are bitwise operation (and why should I care?)

Bitwise Math - ITPedia

Bitwise operations are operations that act on numbers at the bit level. Although bitwise operations have many potential uses for storing one bit and manipulating numbers, the most frequent use at ITP is probably to interface with a shift register and control many more LEDs than you would be able to if using one digital pin for each LED. Binary Before working with bitwise operators, it's important to understand binary numbers. More information can be found at [Wikipedia| The next key concept is that of the most basic logic gates, AND, OR, and NOT. The AND function takes two inputs and returns one. The OR function returns a high value when either or both inputs are high.

The not function only had one input. Examples on a Byte. Double bitwise NOT (~~) Who would have thought that JavaScript’s rare and mysterious bitwise operators could be so useful?

Double bitwise NOT (~~)

I first discovered this trick a while ago from a slideshow on JavaScript performance, by Thomas Fuchs. The “trick” is as follows: The bitwise NOT operator (~) will take its operand, convert it to a 32-bit integer, and will invert each bit so that each 0 becomes a 1 and vice versa. 00000000000000000000000000001001 ...becomes 11111111111111111111111111110110 The effect of this, given the expression ~foo is -(foo + 1). Bit Twiddling Hacks. By Sean Eron Anderson seander@cs.stanford.edu Individually, the code snippets here are in the public domain (unless otherwise noted) — feel free to use them however you please.

Bit Twiddling Hacks

The aggregate collection and descriptions are © 1997-2005 Sean Eron Anderson. The code and descriptions are distributed in the hope that they will be useful, but WITHOUT ANY WARRANTY and without even the implied warranty of merchantability or fitness for a particular purpose. As of May 5, 2005, all the code has been tested thoroughly. Thousands of people have read it. Contents About the operation counting methodology When totaling the number of operations for algorithms here, any C operator is counted as one operation. Compute the sign of an integer The last expression above evaluates to sign = v >> 31 for 32-bit integers. Alternatively, if you prefer the result be either -1 or +1, then use: Bitwise Operations in C. After writing the Game Programming Genesis series, I got a lot of E-mail from people asking me to clarify the bitwise operations I used in the sample programs.

Bitwise Operations in C

I use them all over the place in my code, enough that I don't really think about what I'm writing anymore, and so I neglected the fact that a lot of people don't see them used too often. This article is meant to be a complete introduction to bitwise manipulations using the C programming language. If you don't know C, no big deal. The ideas presented here are very general. Masks and flags using bit fields in .NET. Introduction This article demonstrates a simple use of bit fields as flags for Windows forms.

Masks and flags using bit fields in .NET

Bit fields allow packaging of data into simple structures, and they are particularly useful when bandwidth, memory or data storage is at a premium. This might not appear to be an issue with modern day equipment or every day applications, but we can save up to 16 times more memory and storage when using bit fields instead of other value types such as boolean. Background Storage Consider a boolean value in .NET: Math - Do bitwise operators (other than shifts) make any mathematical sense in base-10. Bit field. Implementation[edit] "A bit field is set up with a structure declaration that labels each field and determines its width.

Bit field