Understanding Bitwise Operations in C Programming

com101b lecture 15 bitwise operations n.w
1 / 10
Embed
Share

Learn about bitwise operations in C programming, including bitwise logical operators, shift operators, bit-fields, and more. Explore examples of Bitwise AND, OR, Exclusive OR, and Complement operations. Understand the precedence and associativity rules for bitwise operators and practice implementing a function to extract a specific bit field from an integer.

  • C Programming
  • Bitwise Operations
  • Bitwise Operators
  • Precedence
  • Associativity

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. COM101B LECTURE 15: BITWISE OPERATIONS ASST. PROF. DR. HACER YALIM KELE REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  2. BITWISE OPS C provides 4 bitwise logical operators, for masking operations on bits; and two bitwise shift operators for shifting bits in a word. C also allows you to partition a word into groups of bits, called bit-fields; and assigns name to them. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  3. BITWISE AND (&) operand1 & operand2 When applied to two integral operands, the binary representations of the values are compared bit by bit. The result is computed as follows: b1 b2 b1&b2 1 1 1 1 0 0 0 1 0 0 0 0 REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  4. BITWISE OR (|) operand1 | operand2 is used to turn the bits on: b1 b2 b1 | b2 1 1 1 1 0 1 0 1 1 0 0 0 REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  5. BITWISE EXCLUSIVE OR (^) b1 b2 b1^b2 1 1 0 1 0 1 0 1 1 0 0 0 REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  6. BITWISE COMPLEMENT (~) Unary operator that yields the one s complement of an integer Example: x = x & ~077 sets the last 6 bits of x to 0. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  7. PRECEDENCE AND ASSOCIATIVITY Precedence order: ~, &, ^, | Example: 01|~01^01&01 is interpreted as: 01|((~01)^(01&01)) Associativity rule: ~: right to left &, ^, |: left to right REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  8. ASSIGNMENT Write a function getbits(x, p, n) that returns the right adjusted n-bit field of x that begins at position p. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  9. BIT-FIELDS C provides a more convenient method for defining and accessing fields within a word than the use of bit-manipulation operators A bit-field is a set of adjacent bits within an implementation-dependent storage unit, called a word . syntax: type field-name: bit-length; REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  10. EXAMPLE struct *An address of operator (&) can not be applied to bit fields { unsigned diskette : 1; unsigned video : 2; *bit-fields can not be dimensioned, like, usigned f:1[5]; unsigned printers : 2; ... unsigned rs232_ports: 3; char* value; // can still hold non-bit-field members } REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

Related


More Related Content