Podcast
Questions and Answers
In C, it's possible to directly manipulate single bits.
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?
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.
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.
A ________ is a byte or word that is applied via bitwise operations to extract or modify a single bit.
What is the purpose of a bitmask in bit manipulation?
What is the purpose of a bitmask in bit manipulation?
In a positive bitmask, a 0-bit indicates the bit to be extracted or modified.
In a positive bitmask, a 0-bit indicates the bit to be extracted or modified.
Which bitwise operation is typically used to set a specific bit to 1?
Which bitwise operation is typically used to set a specific bit to 1?
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?
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?
The bitwise operation used to flip a bit (from 0 to 1, or 1 to 0) is called ________.
The bitwise operation used to flip a bit (from 0 to 1, or 1 to 0) is called ________.
Which operation is commonly used to check if a particular bit is set (i.e., has a value of 1)?
Which operation is commonly used to check if a particular bit is set (i.e., has a value of 1)?
If you are working with bitmaps internally within your program and performance is critical, it's generally better to use MSB as bit 0.
If you are working with bitmaps internally within your program and performance is critical, it's generally better to use MSB as bit 0.
What does the 'reset' operation do to a bit in a bitstream or bitmap?
What does the 'reset' operation do to a bit in a bitstream or bitmap?
In the context of bit manipulation, what is the significance of the number '8' when working with bytes?
In the context of bit manipulation, what is the significance of the number '8' when working with bytes?
To create a negative bitmask for bit b
, you should create a positive bitmask by left-shifting 1 by b
times and then ________ it.
To create a negative bitmask for bit b
, you should create a positive bitmask by left-shifting 1 by b
times and then ________ it.
What is the result of performing a bitwise AND operation between 0b11001010
and 0b10101100
?
What is the result of performing a bitwise AND operation between 0b11001010
and 0b10101100
?
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.
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.
In a negative bitmask, a ______-bit indicates the bit or bits that are to be modified.
In a negative bitmask, a ______-bit indicates the bit or bits that are to be modified.
If char my_byte = 0b11000101;
, what is the value of my_byte
after executing my_byte = my_byte ^ (1 << 2);
?
If char my_byte = 0b11000101;
, what is the value of my_byte
after executing my_byte = my_byte ^ (1 << 2);
?
How many bits are there in a byte?
How many bits are there in a byte?
Typically, operations on bitstreams and bitmaps involve reading or writing multiple bits at a time.
Typically, operations on bitstreams and bitmaps involve reading or writing multiple bits at a time.
Match the bit manipulation operations to their functions:
Match the bit manipulation operations to their functions:
What is the correct C code to create a positive bitmask for bit 3?
What is the correct C code to create a positive bitmask for bit 3?
List the steps involved in modifying a single bit in memory.
List the steps involved in modifying a single bit in memory.
The operation that flips bit i
of bits
is called _______.
The operation that flips bit i
of bits
is called _______.
Bitstreams and Bitmaps are stored in arrays of words only.
Bitstreams and Bitmaps are stored in arrays of words only.
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
?
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
?
In C, given the following code: char byte = 0xb6; int bit = (byte >> 7) & 0x1;
, what is the value of bit
after execution?
In C, given the following code: char byte = 0xb6; int bit = (byte >> 7) & 0x1;
, what is the value of bit
after execution?
When is it preferable to use the MSB as bit 0 when treating an entire sequence of bytes as a bitstream?
When is it preferable to use the MSB as bit 0 when treating an entire sequence of bytes as a bitstream?
CPU instructions can directly manipulate single bits.
CPU instructions can directly manipulate single bits.
The action of switching a bit from 0
to 1
or from 1
to 0
is commonly called ______.
The action of switching a bit from 0
to 1
or from 1
to 0
is commonly called ______.
If you have a word (int) and sizeof(int)
is 4, how do you determine which word contains bit 40?
If you have a word (int) and sizeof(int)
is 4, how do you determine which word contains bit 40?
Describe what a bitstream?
Describe what a bitstream?
A bitmask uses bitwise operations on a byte to affect any other byte independent of their original contents.
A bitmask uses bitwise operations on a byte to affect any other byte independent of their original contents.
In the context of Unix file system permissions, the letters r
, w
, and x
represent the permissions for _______, _______, and _______ respectively.
In the context of Unix file system permissions, the letters r
, w
, and x
represent the permissions for _______, _______, and _______ respectively.
What is the primary function of the following line of C code? char bitmask = ~(1 << 5);
What is the primary function of the following line of C code? char bitmask = ~(1 << 5);
In the context of TCP headers, what is the purpose of the Flags field, and how are individual flags represented?
In the context of TCP headers, what is the purpose of the Flags field, and how are individual flags represented?
In a C program, if int x = 5;
, then the expression x << 2
is equivalent to multiplying x
by 2.
In a C program, if int x = 5;
, then the expression x << 2
is equivalent to multiplying x
by 2.
The bitwise operator |
performs a/an _______ operation.
The bitwise operator |
performs a/an _______ operation.
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?
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?
What is the decimal representation of the binary number 0b00010110
?
What is the decimal representation of the binary number 0b00010110
?
Bitmaps are primarily used for storing audio data.
Bitmaps are primarily used for storing audio data.
A __________ is a sequence of bits packaged into one or more bytes (or words).
A __________ is a sequence of bits packaged into one or more bytes (or words).
Suppose int num = 10;
What will be the output of num >> 1
?
Suppose int num = 10;
What will be the output of num >> 1
?
Briefly describe scenarios where bit manipulation might be preferred over other data manipulation techniques.
Briefly describe scenarios where bit manipulation might be preferred over other data manipulation techniques.
Flashcards
What is a bitstream?
What is a bitstream?
Sequence of bits packaged into one or more bytes/words.
What is a bitmap?
What is a bitmap?
A set of bits packaged into bytes where each bit indicates presence/absence.
What is a bitmask?
What is a bitmask?
A byte/word applied via bitwise operations to extract/modify bits.
What is a positive bitmask?
What is a positive bitmask?
Signup and view all the flashcards
What is a negative bitmask?
What is a negative bitmask?
Signup and view all the flashcards
What is test(bits, i)?
What is test(bits, i)?
Signup and view all the flashcards
What is set(bits, i)?
What is set(bits, i)?
Signup and view all the flashcards
What is reset(bits, i)?
What is reset(bits, i)?
Signup and view all the flashcards
What is toggle(bits, i)?
What is toggle(bits, i)?
Signup and view all the flashcards
When to use MSB as bit 0?
When to use MSB as bit 0?
Signup and view all the flashcards
When to use LSB as bit 0?
When to use LSB as bit 0?
Signup and view all the flashcards
How to create a positive bitmask for bit 'b'?
How to create a positive bitmask for bit 'b'?
Signup and view all the flashcards
How to create a negative bitmask?
How to create a negative bitmask?
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.