Lecture 7 Bit Manipulation in System Programming

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

In C, it's possible to directly manipulate single bits.

False (B)

Which of the following data types are typically used to store bits for manipulation in C?

  • double
  • char
  • float (correct)
  • void

List the four common operations that can be performed on bitstreams or bitmaps.

test, set, reset, toggle

A ________ is a byte or word that is applied via bitwise operations to extract or modify a single bit.

<p>bitmask</p> Signup and view all the answers

What is the purpose of a bitmask in bit manipulation?

<p>To extract or modify a single bit while masking out the other bits (C)</p> Signup and view all the answers

In a positive bitmask, a 0-bit indicates the bit to be extracted or modified.

<p>False (B)</p> Signup and view all the answers

Which bitwise operation is typically used to set a specific bit to 1?

<p>AND (B)</p> Signup and view all the answers

If you have a byte with the value 0b10101010 and you want to reset (set to 0) the 3rd bit (counting from the right, starting at 0), what bitmask should you use?

<p>0b11110111</p> Signup and view all the answers

The bitwise operation used to flip a bit (from 0 to 1, or 1 to 0) is called ________.

<p>toggle</p> Signup and view all the answers

Which operation is commonly used to check if a particular bit is set (i.e., has a value of 1)?

<p>Set (C)</p> Signup and view all the answers

If you are working with bitmaps internally within your program and performance is critical, it's generally better to use MSB as bit 0.

<p>False (B)</p> Signup and view all the answers

What does the 'reset' operation do to a bit in a bitstream or bitmap?

<p>Changes the bit to 0 (B)</p> Signup and view all the answers

In the context of bit manipulation, what is the significance of the number '8' when working with bytes?

<p>bits per byte</p> Signup and view all the answers

To create a negative bitmask for bit b, you should create a positive bitmask by left-shifting 1 by b times and then ________ it.

<p>negate</p> Signup and view all the answers

What is the result of performing a bitwise AND operation between 0b11001010 and 0b10101100?

<p>0b01100110 (B)</p> Signup and view all the answers

When using words (int or long int) to store bits, the calculation to find which word contains a bit is independent of the sizeof the data type.

<p>False (B)</p> Signup and view all the answers

In a negative bitmask, a ______-bit indicates the bit or bits that are to be modified.

<p>0</p> Signup and view all the answers

If char my_byte = 0b11000101;, what is the value of my_byte after executing my_byte = my_byte ^ (1 << 2);?

<p>0b11000101 (B)</p> Signup and view all the answers

How many bits are there in a byte?

<p>8</p> Signup and view all the answers

Typically, operations on bitstreams and bitmaps involve reading or writing multiple bits at a time.

<p>False (B)</p> Signup and view all the answers

Match the bit manipulation operations to their functions:

<p>test = Returns the value of a specified bit. set = Changes a specified bit to 1. reset = Changes a specified bit to 0. toggle = Flips the value of a specified bit.</p> Signup and view all the answers

What is the correct C code to create a positive bitmask for bit 3?

<p><code>char bitmask = 1 &gt;&gt; 3;</code> (C)</p> Signup and view all the answers

List the steps involved in modifying a single bit in memory.

<p>Determine the byte/word, Load byte/word from memory, Extract value if needed, Modify the specific bit, Write the byte/word back to memory</p> Signup and view all the answers

The operation that flips bit i of bits is called _______.

<p>toggle(bits, i)</p> Signup and view all the answers

Bitstreams and Bitmaps are stored in arrays of words only.

<p>False (B)</p> Signup and view all the answers

If you have char data = 0b10110011, and you apply the bitmask mask = 0b00001111 using the bitwise AND operation (result = data & mask), what will be the value of result?

<p>0b10110000 (C)</p> Signup and view all the answers

In C, given the following code: char byte = 0xb6; int bit = (byte >> 7) & 0x1;, what is the value of bit after execution?

<p>1</p> Signup and view all the answers

When is it preferable to use the MSB as bit 0 when treating an entire sequence of bytes as a bitstream?

<p>When you need to debug your program using byte streams. (B)</p> Signup and view all the answers

CPU instructions can directly manipulate single bits.

<p>False (B)</p> Signup and view all the answers

The action of switching a bit from 0 to 1 or from 1 to 0 is commonly called ______.

<p>toggling</p> Signup and view all the answers

If you have a word (int) and sizeof(int) is 4, how do you determine which word contains bit 40?

<p>Multiply 40 by 4 (C)</p> Signup and view all the answers

Describe what a bitstream?

<p>sequence of bits packaged into one or more bytes/words</p> Signup and view all the answers

A bitmask uses bitwise operations on a byte to affect any other byte independent of their original contents.

<p>False (B)</p> Signup and view all the answers

In the context of Unix file system permissions, the letters r, w, and x represent the permissions for _______, _______, and _______ respectively.

<p>read, write, execute</p> Signup and view all the answers

What is the primary function of the following line of C code? char bitmask = ~(1 << 5);

<p>Set bit 5 to 0. (C)</p> Signup and view all the answers

In the context of TCP headers, what is the purpose of the Flags field, and how are individual flags represented?

<p>represents boolean states, single bit</p> Signup and view all the answers

In a C program, if int x = 5;, then the expression x << 2 is equivalent to multiplying x by 2.

<p>False (B)</p> Signup and view all the answers

The bitwise operator | performs a/an _______ operation.

<p>OR</p> Signup and view all the answers

Given the C declaration char bitmap[4] = {0x41, 0x42, 0x43, 0x44};, what code would test if bit 6 of the second byte (index 1) is set?

<p><code>if (bitmap[1] &gt;&gt; 6)</code> (A)</p> Signup and view all the answers

What is the decimal representation of the binary number 0b00010110?

<p>22</p> Signup and view all the answers

Bitmaps are primarily used for storing audio data.

<p>False (B)</p> Signup and view all the answers

A __________ is a sequence of bits packaged into one or more bytes (or words).

<p>bitstream</p> Signup and view all the answers

Suppose int num = 10; What will be the output of num >> 1?

<p>20 (C)</p> Signup and view all the answers

Briefly describe scenarios where bit manipulation might be preferred over other data manipulation techniques.

<p>memory efficiency, low level operations, embedded systems</p> Signup and view all the answers

Flashcards

What is a bitstream?

Sequence of bits packaged into one or more bytes/words.

What is a bitmap?

A set of bits packaged into bytes where each bit indicates presence/absence.

What is a bitmask?

A byte/word applied via bitwise operations to extract/modify bits.

What is a positive bitmask?

Bitmask where a 1-bit indicates the bit(s) to extract/modify.

Signup and view all the flashcards

What is a negative bitmask?

Bitmask where a 0-bit indicates the bit(s) to modify.

Signup and view all the flashcards

What is test(bits, i)?

Returns the value of bit i of bits.

Signup and view all the flashcards

What is set(bits, i)?

Changes bit i of bits to 1.

Signup and view all the flashcards

What is reset(bits, i)?

Changes bit i of bits to 0.

Signup and view all the flashcards

What is toggle(bits, i)?

Flips bit i of bits. 0 becomes 1, and 1 becomes 0.

Signup and view all the flashcards

When to use MSB as bit 0?

MSB is bit 0 if debugging byte streams.

Signup and view all the flashcards

When to use LSB as bit 0?

Internally using bitmaps

Signup and view all the flashcards

How to create a positive bitmask for bit 'b'?

Shift 1 left b times.

Signup and view all the flashcards

How to create a negative bitmask?

Create positive bitmask & negate it.

Signup and view all the flashcards

Study Notes

  • CSCI 2122 focuses on bit manipulation in system programming

Key Ideas: Bits and Bytes

  • CPU instructions work with registers containing multiple bits (8, 16, 32, 64).
  • CPU instructions do not directly handle single bits.
  • C and similar languages cannot directly manipulate single bits.
  • Bits should be stored in bytes (char) or words (int, long int).
  • Bytes/words are manipulated to test, toggle, set, or reset specific bits.

Reading and Writing Bits

  • To read a single bit, determine the byte/word containing the bit, load the byte/word from memory, and extract the bit's value.
  • To modify a single bit:
  • Find the relevant byte or word.
  • Load the byte or word from memory.
  • Extract the bit value if necessary.
  • Alter the byte/word to change the specific bit.
  • Write the updated byte/word back to memory.

Bitstreams vs Bitmaps

  • A bitstream is a sequence of bits packed into one or more bytes or words.
  • Example: char stream[] = {0xb6, 0x57, 0x1c, 0xd0}.
  • A bitmap uses bits in bytes to indicate presence or absence of elements in a set, so a 1-bit means item present, 0-bit means item absent.

Real World examples

  • TCP header Flags field uses single bits to indicate Boolean states.
  • Unix file permissions use a bitmap for read (r), write (w), and execute (x) rights for Owner, Group, and Others.
  • RGBA color channels use bits, such as 0xFFFFFF00 for 100% opaque Yellow.

Bit Ordering

  • When treating a sequence of bytes as a bitstream, it is important which bit is bit 0
  • There is an important differentiation between MSB (Most Significant Bit) and BSB (Least Significant Bit)

MSB vs LSB

  • If the MSB of byte 0 is bit 0: bit0 = (bytes[0] >> 7) & 0x1;
  • If the LSB of byte 0 is bit 0: bit0 = bytes[0] & 0x1;

Usage of MSB vs LSB

  • If specified, follow that specification
  • MSB as bit 0 is best for debugging byte streams
  • Use LSB as bit 0 if the program uses bitmaps internally

Finding Bits

  • In a bitstream/bitmap of n bytes, bit B is in byte B / 8.
  • All bytes are the same size
  • Example: For the byte array {0xb6, 0x57, 0x1c, 0xd0}, bit 4 is in byte 0 (4 / 8 = 0), bit 30 is in byte 3 (30 / 8 = 3), and bit 17 is in byte 2 (17 / 8 = 2).
  • When using words (int or long int), divide by sizeof(word) * 8

Bitmasks Defined

  • A bitmask is a byte/word used with bitwise operations to extract/modify a specific bit by "masking" others, with positive and negative types.

Bitmask Types

  • Positive bitmask: a 1-bit indicates the bit(s) to extract/modify (e.g., 00100000 or 0x20 to manipulate bit 5).
  • Negative bitmask: a 0-bit indicates the bit(s) to modify (e.g., 11011111 or 0xdf to manipulate bit 5).
  • A negative mask is the bitwise negation of a positive mask.

Creating Bitmasks

  • For bit b, a positive bitmask is created by shifting 1 left b times: 1 << b.
  • Example: for bitmask 00100000, char bitmask = 1 << 5;
  • A negative bitmask is created by creating a positive mask and negating it: ~(1 << b).
  • Example: char bitmask = ~(1 << 5); would create 11011111

Common Operations

  • Test(bits, i): returns the value of bit i of bits.
  • Set(bits, i): makes bit I of bits to 1.
  • Reset(bits, i): makes bit I of bits to 0.
  • Toggle(bits, i): flips bit i of bits.
  • The operations all compute the target bit's byte/word, the bit's location in the byte/word, the bitmask, and then perform the operation with the bitmask

Test and Set Code

  • Is bit 0 the MSB or LSB?
  • Test:
  • Locate byte of target bit, int idx = b / 8;
  • Locate bit in byte, int bidx = b % 8;
  • Compute mask to isolate bit, char mask = 1 << bidx;
  • Test if the bit is not 0, return bits[idx] & mask != 0;
  • Set:
  • Locate byte of target bit, int idx = b / 8;
  • Locate bit in byte, int bidx = b % 8;
  • Compute mask to set bit, char mask = 1 << bidx;
  • Set bit to 1 by ORing with the mask, bits[idx] = bits[idx] | mask;

Reset and Toggle Code

  • Reset:
  • Locate byte of target bit, int idx = b / 8;
  • Locate bit in byte, int bidx = b % 8;
  • Compute mask to reset bit, char mask = ~(1 << bidx);
  • Set bit to 0 by ANDing with mask, bits[idx] = bits[idx] & mask;
  • Toggle:
  • Locate byte of target bit, int idx = b / 8;
  • Locate bit in byte, int bidx = b % 8;
  • Compute mask to set bit, char mask = 1 << bidx;
  • Set bit to 0 by XORing with mask, bits[idx] = bits[idx] ^ mask;

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

COBS Quiz
10 questions

COBS Quiz

UndisputedMoldavite avatar
UndisputedMoldavite
Use Quizgecko on...
Browser
Browser