music

# Boolean programming and bit operations

## summary

This chapter looks at Boolean programming, Boolean logic, and bit operations.

## free computer programming text book project

If you like the idea of this project,

### stub section

This subchapter is a stub section. It will be filled in with instructional material later. For now it serves the purpose of a place holder for the order of instruction.

Professors are invited to give feedback on both the proposed contents and the propsed order of this text book. Send commentary to Milo, PO Box 1361, Tustin, California, 92781, USA.

# Boolean programming and bit operations

This chapter looks at Boolean programming, Boolean logic, and bit operations.

Boolean algebra is named for George Boole, who introduced the ideas in the 1854 work “An Investigation of the Law of Thought”. Claude Shannon showed the application of Boolean algebra to switching circuits in the 1938 work “Symbolic Analysis of Relay and Switching Circuits”.

Please note the differences between a logical Boolean operation, an integer Boolean operation, and a bit-wise Boolean operation. Note that terminology varies, so you will eventually see references using these terms different than presented here.

In the case of a logical Boolean operation, the result is a logical BOOLEAN type of either TRUE or FALSE. Some languages, such as C, do not have a built-in Boolean type. There are several different and incompatible encodings for TRUE and FALSE (see table below).

In the case of an integer Boolean operation, the operation is performed on the two integers as whole numbers. The result can be inaccurate if the integer is not in a specified form (the legal options usually being [ZERO and ONE] or [ZERO and NEGATIVE ONE]. This works best with -1 and 0 as TURE and FALSE (or vice versa). There are several different and incompatible encodings for TRUE and FALSE (see table below).

somelanguages TRUE FALSE C 1 0 C non-zero zero negative zero orpositive -1 0 0 1

in the case of a bit-wise Boolean operation, a logical operation is performed on each corresponding bit of the two bit strings (or two single bits). Note that a NOT is performed on only a single bit string or single bit.

## Stanford C essentials

Stanford CS Education Library This [the following section until marked as end of Stanford University items] is document #101, Essential C, in the Stanford CS Education Library. This and other educational materials are available for free at http://cslibrary.stanford.edu/. This article is free to be used, reproduced, excerpted, retransmitted, or sold so long as this notice is clearly reproduced at its beginning. Copyright 1996-2003, Nick Parlante, nick.parlante@cs.stanford.edu.

### Bitwise Operators

C includes operators to manipulate memory at the bit level. This is useful for writing lowlevel hardware or operating system code where the ordinary abstractions of numbers, characters, pointers, etc.… are insufficient -- an increasingly rare need. Bit manipulation code tends to be less “portable”. Code is “portable” if with no programmer intervention it compiles and runs correctly on different types of computers. The bitwise operations are typically used with unsigned types. In particular, the shift operations are guaranteed to shift 0 bits into the newly vacated positions when used on unsigned values.

 ~ Bitwise Negation (unary) Ð flip 0 to 1 and 1 to 0 throughout & Bitwise And | Bitwise Or ^ Bitwise Exclusive Or >> Right Shift by right hand side (RHS) (divide by power of 2) << Left Shift by RHS (multiply by power of 2)

Do not confuse the Bitwise operators with the logical operators. The bitwise connectives are one character wide (&, |) while the boolean connectives are two characters wide (&&, ||). The bitwise operators have higher precedence than the boolean operators. The compiler will never help you out with a type error if you use & when you meant &&. As far as the type checker is concerned, they are identical-- they both take and produce integers since there is no distinct boolean type.

Stanford CS Education Library This [the above section] is document #101, Essential C, in the Stanford CS Education Library. This and other educational materials are available for free at http://cslibrary.stanford.edu/. This article is free to be used, reproduced, excerpted, retransmitted, or sold so long as this notice is clearly reproduced at its beginning. Copyright 1996-2003, Nick Parlante, nick.parlante@cs.stanford.edu.

### end of Stanford C essentials

“31 Every object in the language has a type, which characterizes a set of values and a set of applicable operations. The main classes of types are elementary types (comprising enumeration, numeric, and access types) and composite types (including array and record types).” —:Ada-Europe’s Ada Reference Manual: Introduction: Language Summary See legal information

“32/2 An enumeration type defines an ordered set of distinct enumeration literals, for example a list of states or an alphabet of characters. The enumeration types Boolean, Character, Wide_Character, and Wide_Wide_Character are predefined.” —:Ada-Europe’s Ada Reference Manual: Introduction: Language Summary See legal information

## JOVIAL

The following material is from the unclassified Computer Programming Manual for the JOVIAL (J73) Language, RADC-TR-81-143, Final Technical Report of June 1981.

1.1.5 Built-In Functions

The JOVIAL built-in functions provide advanced, specialized
operations that are not covered by the JOVIAL operators.

BIT(b,i,n)     A string of n bits starting at the i'th bit
of the bit string b

SHIFTL(b,n)    Bit string b shifted left by n bits
SHIFTR(b,n)    Bit string b shifted right by n bits

Chapter 1 Introduction, page 9

# free music player coding example

Coding example: I am making heavily documented and explained open source code for a method to play music for free — almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).

View music player in action: www.musicinpublic.com/.

Create your own copy from the original source code/ (presented for learning programming).

## view text bookHTML file

Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.

 previous page next page
 Tweets by @osdata

## free computer programming text book project

Building a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming.

If you like the idea of this project,

send donations to:
Milo
PO Box 1361
Tustin, California 92781

Supporting the entire project:

If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church)

Some or all of the material on this web page appears in the

This web site handcrafted on Macintosh computers using Tom Bender’s Tex-Edit Plus and served using FreeBSD .

†UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.

Names and logos of various OSs are trademarks of their respective owners.