1 / 11

CET 3510 – Lecture 11

CET 3510 – Lecture 11. Bit Manipulation in a High-Level Programming Language Dr. José M. Reyes Álamo. Bitwise Operations. A logical operation that is performed between bit of the operand(s) in their corresponding position Bitwise AND Bitwise OR Bitwise NOT Bitwise XOR. Bitwise Operations.

baird
Download Presentation

CET 3510 – Lecture 11

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. CET 3510 – Lecture 11 Bit Manipulation in a High-Level Programming Language Dr. José M. Reyes Álamo

  2. Bitwise Operations • A logical operation that is performed between bit of the operand(s) in their corresponding position • Bitwise AND • Bitwise OR • Bitwise NOT • Bitwise XOR

  3. Bitwise Operations • Recipe: • Move the first operand into an appropriate register • Perform the corresponding operation proving the first and second operand • HLA Code Example: • mov( eax, ecx ); • and( ebx, ecx ); • mov( eax, ecx ); • or( ebx, ecx ); • mov( eax, ecx ); • xor( ebx, ecx ); • mov( ebx, ecx ); • not( ecx ); //BE CAREFUL

  4. Bitwise Operations in C/C++ • Result is the same, syntax is different • Bitwise AND operator: & • Bitwise OR operator: | • Bitwise NOT operator: ~ • Bitwise XOR operator: ^ • Be careful: • Do not confuse these with the logical comparison operators: &&, ||, !

  5. Bitwise Operations in C/C++ • Code Example: void logicalOperations(){ int x, y, z; x = 5; y = 2; z = x & y; cout << "x & y = " << z << endl; z = x | y; cout << "x | y = " << z << endl; z = x ^ y; cout << "x ^ y = " << z << endl; z = ~x; cout << "~x = " << z << endl; system("PAUSE"); }

  6. Shifting Left in HLA • Shift left move each bit n positions to the left • Lower order bits becomes a 0 • Higher order bit become a carry out • HLA code: • shl( count, dest ) | count = positions to shift; dest = register or variable • Shifting n places to the left one position is equivalent to multiplying by the base (radix) n times (radix)

  7. Shifting Right in HLA • Shift right move each bit n positions to the right • Higher order bits becomes a 0 • Lower order bit become a carry out • HLA code: • shr( count, dest ) | count = positions to shift; dest = register or variable • sar( count, dest ) | count = positions to shift; dest = register or variable. Used for arithmetic shifting. • Shifting n places to the right one position is equivalent to dividing by the base (radix) n times

  8. Shifting in C/C++ • The bitwise shifting operators are • Right shift (>>) • Left shift (<<) • Be careful with the compiler as shifting (especially shifting right) might return unexpected results

  9. Shifting in C++ • Code Example: void shiftingOperations(){ int x, y, z; x = 2; x = x << 5; cout << "x shifted left = " << x << endl; x = x >> 1; cout << "x shifted right = " << x << endl; }

  10. Bit Manipulation Example HLA • HLA Code: stdout.put( “Enter the current month, day, and year: “ ); stdin.get( month, day, year ); mov( 0, ax ); mov( ax, packedDate ); //Just in case there is an error. mov( month, al ); shl( 5, ax ); or( day, al ); shl( 7, ax ); or( year, al ); mov( ax, packedDate ); stdout.put( “Packed data = $”, packedDate, nl );

  11. Bit Manipulation Example C++ void packedDate(){ int month, day, year, packedDate; cout << "Enter the current month, day, and year: " << endl; cin >> month >> day >> year; packedDate = 0; packedDate = packedDate | month; packedDate = packedDate << 5; packedDate = packedDate | day; packedDate = packedDate << 7; packedDate = packedDate | year; cout << "Packed date is " << hex << packedDate << endl; }

More Related