1 / 15

Bitwise Operators Fall 2008

Bitwise Operators Fall 2008. Dr. David A. Gaitros dgaitros@admin.fsu.edu. Bitwise Operators. “Mathematics may be the language of scientists, but binary is the language of Computer Scientists” Dr. David A. Gaitros. Binary Operations. Recall the following:

leachj
Download Presentation

Bitwise Operators Fall 2008

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bitwise OperatorsFall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu

  2. Bitwise Operators “Mathematics may be the language of scientists, but binary is the language of Computer Scientists” Dr. David A. Gaitros

  3. Binary Operations • Recall the following: • A bit is the smallest digit capable of being stored in a modern day digital computer. • A byte, consisting of 8 bits is the smallest unit of storage that a computer can work with. • A word ( usually somewhere between 32 and 64 bits) is the smallest addressable item in a computer. • The word size of a computer is usually dictated by either the bus size of the system or the word size of the CPU.

  4. Bitwise Operators

  5. Bitwise Operators • Bitwise operators are used to directly manipulate the bits of integral operands such as char, short, int, long (both signed and unsined). • Normally unsigned integers are used when dealing with bitwise operations.

  6. Bitwise Operations • Code Example: #include <iostream> #include <iomanip> using namespace std; int main(void) { unsigned x=10; unsigned y=0; y = x | y; return 0; } // What does this do? // Nothing really.

  7. Bitwise Operators • Shortcuts X &= y; same as x = x & y; x |= y; same as x = x | y; x ^= y; same as x = x ^ y; x << y; same as x = x << y; x >> y; same as x = x>> y;

  8. Bitwise Operations • Why use them? • A single bit shift left is a very fast way of integer multiplication by 2. • A single bit shift right is a very fact way of integer division by 2. • An AND operation with all zeros is a very efficient way to clear out a field. • We can use single bits in a word (32 bits) to represent Boolean values very efficiently. These are sometimes call flags.

  9. Bitwise Operators short x = 6891; short y = 11318; Internal Representation x: 00011010 11101011 y: 00101100 00110110

  10. Bitwise Operators // Logical AND operator x: 00011010 11101011 y: 00101100 00110110 -------------------- 00001000 00100010

  11. Bitwise Operators // Logical OR operator x: 00011010 11101011 y: 00101100 00110110 ----------------- 00111110 11111111

  12. Bitwise Operators // Logical XOR // (exclusive OR) x: 00011010 11101011 y: 00101100 00110110 ------------------- 00110110 11011101

  13. Bitwise Operators // Shift Left 2 x: 00011010 11101011 ------------------------ 01101011 10101100 // Shift right 4 y: 00101100 00110110 ----------------- 00000010 11000011

  14. Bitwise Operators // And here is the // complement of x (~x) x: 00011010 11101011 ------------------ ~x: 11100101 00010100

  15. Bitwise Operators // So.. How do I set // a bit? void SetBit(int num) { int mask=1<<num; flags = flags | mask; }

More Related