Podcast
Questions and Answers
In C, single bits can be directly manipulated using CPU instructions.
In C, single bits can be directly manipulated using CPU instructions.
False (B)
Which data types are typically used in C to store bits for bit manipulation?
Which data types are typically used in C to store bits for bit manipulation?
- char, int, and long int (correct)
- struct and union
- float and double
- void and pointer types
List the three steps required to read a single bit from memory.
List the three steps required to read a single bit from memory.
- Determine the byte or word that contains the bit.
- Load the byte or word from memory.
- Extract the value of the bit from the byte or word.
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.
What does a 1-bit indicate in a bitmap?
What does a 1-bit indicate in a bitmap?
In a TCP header, each flag in the Flags field represents multiple boolean states using a single bit.
In a TCP header, each flag in the Flags field represents multiple boolean states using a single bit.
In Unix file system permissions, what is a bitmap used for?
In Unix file system permissions, what is a bitmap used for?
If the problem specification doesn't define which bit to use, using the ______ as bit 0 is useful for debugging programs that read in byte streams.
If the problem specification doesn't define which bit to use, using the ______ as bit 0 is useful for debugging programs that read in byte streams.
When should the Least Significant Bit (LSB) be used as bit 0?
When should the Least Significant Bit (LSB) be used as bit 0?
All bytes hold a different number of bits depending on the system architecture.
All bytes hold a different number of bits depending on the system architecture.
In a bit stream of n bytes, provide the formula to find the byte that contains bit B.
In a bit stream of n bytes, provide the formula to find the byte that contains bit B.
If using words (int or long int) to store bits, the bit location is determined by dividing by the sizeof the word multiplied by ______.
If using words (int or long int) to store bits, the bit location is determined by dividing by the sizeof the word multiplied by ______.
What is a bitmask?
What is a bitmask?
A negative bitmask has a 1-bit to indicate bits to be extracted or modified.
A negative bitmask has a 1-bit to indicate bits to be extracted or modified.
Explain how to create a positive bitmask for bit 'b'.
Explain how to create a positive bitmask for bit 'b'.
To create a negative bitmask, create a positive bitmask and then ______ it.
To create a negative bitmask, create a positive bitmask and then ______ it.
Which operation returns the value of bit i of bits?
Which operation returns the value of bit i of bits?
toggle(bits, i)
sets bit i to 0, regardless of its original value.
toggle(bits, i)
sets bit i to 0, regardless of its original value.
List the four steps that bit operations generally follow.
List the four steps that bit operations generally follow.
In the set
operation, the bit is set to 1 using the ______ bitwise operation with the mask.
In the set
operation, the bit is set to 1 using the ______ bitwise operation with the mask.
Match the bit manipulation operations with their descriptions:
Match the bit manipulation operations with their descriptions:
Which bitwise operation is used in the 'reset' operation to set a specific bit to 0?
Which bitwise operation is used in the 'reset' operation to set a specific bit to 0?
Bitstreams and bitmaps differ significantly in how they are stored, with bitstreams stored in arrays of bytes and bitmaps stored in linked lists.
Bitstreams and bitmaps differ significantly in how they are stored, with bitstreams stored in arrays of bytes and bitmaps stored in linked lists.
What is the significance of identifying both the byte and the location of bit within the byte?
What is the significance of identifying both the byte and the location of bit within the byte?
In bit manipulation, registers are manipulated as collections of bits; common sizes include 8(bytes), 16, 32, and ______.
In bit manipulation, registers are manipulated as collections of bits; common sizes include 8(bytes), 16, 32, and ______.
Consider the byte 0x3F
. If you perform a left bit shift operation by 2 (0x3F << 2
), what is the resulting byte?
Consider the byte 0x3F
. If you perform a left bit shift operation by 2 (0x3F << 2
), what is the resulting byte?
When toggling a bit in a byte, the XOR (^
) operation always sets the bit to 1
regardless of its initial state.
When toggling a bit in a byte, the XOR (^
) operation always sets the bit to 1
regardless of its initial state.
If sizeof(int)
is 4 bytes, how many bits does that store?
If sizeof(int)
is 4 bytes, how many bits does that store?
Describe the purpose of applying bitmasks through bitwise operations in the context of bitstream or bitmap.
Describe the purpose of applying bitmasks through bitwise operations in the context of bitstream or bitmap.
Given the bitstream 01101001
, applying a bitmask of 00000001
using the AND operation would ______ bit.
Given the bitstream 01101001
, applying a bitmask of 00000001
using the AND operation would ______ bit.
A bitstream is always stored as a contiguous sequence of bits, without any padding or alignment requirements.
A bitstream is always stored as a contiguous sequence of bits, without any padding or alignment requirements.
In the function int test(char *bytes, int bit)
, what happens if you change int idx = bit / 8
to int idx = bit % 8
?
In the function int test(char *bytes, int bit)
, what happens if you change int idx = bit / 8
to int idx = bit % 8
?
Given char stream[] = {0xb6, 0x57, 0x1c, 0xd0};
, finding bit 22 involves indexing element ______ of the stream
.
Given char stream[] = {0xb6, 0x57, 0x1c, 0xd0};
, finding bit 22 involves indexing element ______ of the stream
.
If 0b11001010
represents an RGB color with the most significant bit indicates opacity and the following 3 bits being red, which operation reveals whether the color is fully opaque?
If 0b11001010
represents an RGB color with the most significant bit indicates opacity and the following 3 bits being red, which operation reveals whether the color is fully opaque?
When the problem specification doesn't direct you, adopting the endianness typical of the system ensures maximal portability.
When the problem specification doesn't direct you, adopting the endianness typical of the system ensures maximal portability.
Explain the difference between setting and toggling a bit.
Explain the difference between setting and toggling a bit.
When shifting 1 to the left by n
bits for generating a bitmask, the operation is written as 1 ______ n
in the C-style syntax.
When shifting 1 to the left by n
bits for generating a bitmask, the operation is written as 1 ______ n
in the C-style syntax.
An image is 300 pixels wide, represented as a bitstream where each pixel demands exactly one bit. How many bytes encapsulate one row?
An image is 300 pixels wide, represented as a bitstream where each pixel demands exactly one bit. How many bytes encapsulate one row?
Bitmaps offer lossless scaling because the vector-based formats store a continuous range of values across every zoom.
Bitmaps offer lossless scaling because the vector-based formats store a continuous range of values across every zoom.
In C, explain what happens if you attempt to directly assign a byte (char myByte
) the floating-point value of 3.1415
.
In C, explain what happens if you attempt to directly assign a byte (char myByte
) the floating-point value of 3.1415
.
Dr. Brodsky developed all slides unless ______ otherwise.
Dr. Brodsky developed all slides unless ______ otherwise.
Flashcards
What is a bitstream?
What is a bitstream?
A sequence of bits packaged into one or more bytes/words.
What is a bitmap?
What is a bitmap?
Set of bits packaged into one or more bytes where each bit represents presence/absence.
What is a bitmask?
What is a bitmask?
A byte/word applied via bitwise operations to extract or modify a single bit.
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 does test(bits, i) do?
What does test(bits, i) do?
Signup and view all the flashcards
What does set(bits, i) do?
What does set(bits, i) do?
Signup and view all the flashcards
What does reset(bits, i) do?
What does reset(bits, i) do?
Signup and view all the flashcards
What does toggle(bits, i) do?
What does toggle(bits, i) do?
Signup and view all the flashcards
What is the general structure of bit operations?
What is the general structure of bit operations?
Signup and view all the flashcards
Why determine the byte/word?
Why determine the byte/word?
Signup and view all the flashcards
Study Notes
- Dr. Brodsky developed the slides and has given permission for their use.
Lecture Contents
- Focus is on manipulating bits
- The topics include how bits are stored, identifying bytes, bitmasks, and the four bit operations.
- These operations consist of test, set, reset, and toggle.
- The reading for the lecture is Chapter 2.2
- Bits cannot be directly manipulated, but bytes or words that store bits can be.
- CPU instructions work with registers containing multiple bits (8, 16, 32, or 64).
- In C, direct manipulation of single bits is not possible.
- Bytes (char) or words (int, long int) are used to store bits.
- Bytes and words must be manipulated to test, toggle, set, and reset bits.
- To read a single bit, the byte or word containing the bit must be identified, loaded from memory, and the value of the bit extracted.
- To modify a single bit, the byte or word containing the bit is determined, loaded, its value extracted if needed, the byte or word modified, then written back to memory.
Bitstreams and Bitmaps
- A bitstream is a sequence of bits packaged into bytes or words.
- An example is
char stream[] = {0xb6, 0x57, 0x1c, 0xd0};
- A bitmap is a set of bits packaged into bytes where each bit indicates the presence or absence of an element in a set.
- A 1-bit indicates the presence of an element, while a 0-bit indicates its absence.
- Flags in a TCP header are a single bit, either set (1) or unset (0).
- Multiple flags can be set simultaneously
- Used to represent multiple Boolean states within a single byte.
- A bitstream consists of a sequence of bytes:
0xb6, 0x57, 0x1c, 0xd0
- The bytes are ordered naturally.
- The value of whether bit 0 is the MSB or LSB must be determined
MSB vs LSB as Bit 0
-
If the MSB of byte 0 is bit 0 of the bitstream, then the LSB (bit 7) is considered bit 0,
bit0 = (bytes[0] >> 7) & 0x1;
-
If the LSB of byte 0 is bit 0 of the bitstream, then the MSB (bit 0) is considered bit and
bit0 = bytes[0] & 0x1;
-
The difference comes down to whether you want the MSB/LSB to start at array index 0
-
If a problem specifies which to use, you must follow those.
-
If byte streams being read into a program require debugging, use the MSB as bit 0.
-
If the program uses bitmaps internally, use LSB as bit 0.
-
In a bitstream or bitmap of
n
bytes, bitB
is contained in byteB / 8
. -
char bytes[] = {0xb6, 0x57, 0x1c, 0xd0}
Bits in a Word
- If words (int or long int) store bits instead of bytes, divide by
sizeof
the word, multiplied by 8 int words[] = {0xb669a442, 0x57af9101, 0x1cbe191f, 0xd00191ae}
- If
sizeof(int)
is 4, then the location of Bit 4 for example is4 / (sizeof(int) * 8) = 4 / 32 = 0
Bitmasks
- A bitmask is a byte or word applied with bitwise operations to a byte or word in a bitstream/bitmap
- A bitmask is applied to extract or modify a single bit by masking out other bits.
- The are positive and negative bitmasks.
Positive and Negative Bitmasks
- In a positive bitmask, a 1-bit indicates the bit(s) to extract or modify.
- In a negative bitmask, a 0-bit indicates the bit(s) to modify
- A negative mask is a bitwise negation of a positive mask.
- To create a positive bitmask for bit
b
, shift1
leftb
timeschar bitmask = ~(1 << b);
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Lecture focuses on manipulating bits including how bits are stored. Topics include identifying bytes, bitmasks, and the four bit operations: test, set, reset, and toggle. The reading material for this lecture is Chapter 2.2.