cet 3510 lecture 11 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CET 3510 – Lecture 11 PowerPoint Presentation
Download Presentation
CET 3510 – Lecture 11

Loading in 2 Seconds...

play fullscreen
1 / 11

CET 3510 – Lecture 11 - PowerPoint PPT Presentation


  • 138 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CET 3510 – Lecture 11' - baird


Download Now 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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cet 3510 lecture 11

CET 3510 – Lecture 11

Bit Manipulation in a High-Level Programming Language

Dr. José M. Reyes Álamo

bitwise operations
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 operations1
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
bitwise operations in c c
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: &&, ||, !
bitwise operations in c c1
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");

}

shifting left in hla
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)
shifting right in hla
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
shifting in c c
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
shifting in c
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;

}

bit manipulation example hla
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 );

bit manipulation example c
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;

}