Lecture 7 Bits and Bitwise Operations
41 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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?

  • 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.

  1. Determine the byte or word that contains the bit.
  2. Load the byte or word from memory.
  3. 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.

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

What does a 1-bit indicate in a bitmap?

<p>The corresponding element is present in the set. (B)</p> Signup and view all the answers

In a TCP header, each flag in the Flags field represents multiple boolean states using a single bit.

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

In Unix file system permissions, what is a bitmap used for?

<p>A bitmap is used to control read, write, and execute permissions for Owner, Group, and Others.</p> Signup and view all the answers

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.

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

When should the Least Significant Bit (LSB) be used as bit 0?

<p>When the program exclusively uses bitmaps internally. (A)</p> Signup and view all the answers

All bytes hold a different number of bits depending on the system architecture.

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

In a bit stream of n bytes, provide the formula to find the byte that contains bit B.

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

If using words (int or long int) to store bits, the bit location is determined by dividing by the sizeof the word multiplied by ______.

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

What is a bitmask?

<p>A byte or word used in bitwise operations to extract or modify bits. (A)</p> Signup and view all the answers

A negative bitmask has a 1-bit to indicate bits to be extracted or modified.

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

Explain how to create a positive bitmask for bit 'b'.

<p>Shift 1 left 'b' times (1 &lt;&lt; b).</p> Signup and view all the answers

To create a negative bitmask, create a positive bitmask and then ______ it.

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

Which operation returns the value of bit i of bits?

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

toggle(bits, i) sets bit i to 0, regardless of its original value.

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

List the four steps that bit operations generally follow.

<ol> <li>Compute the byte or word of the target bit.</li> <li>Compute the location of the bit in the byte or word.</li> <li>Compute the bitmask used to perform the desired operation.</li> <li>Perform the operation using the bitmask.</li> </ol> Signup and view all the answers

In the set operation, the bit is set to 1 using the ______ bitwise operation with the mask.

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

Match the bit manipulation operations with their descriptions:

<p>Test = Return the value of a specific bit. Set = Change a specific bit to 1. Reset = Change a specific bit to 0. Toggle = Flip a specific bit.</p> Signup and view all the answers

Which bitwise operation is used in the 'reset' operation to set a specific bit to 0?

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

Bitstreams and bitmaps differ significantly in how they are stored, with bitstreams stored in arrays of bytes and bitmaps stored in linked lists.

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

What is the significance of identifying both the byte and the location of bit within the byte?

<p>It's crucial to know the byte and location of the bit within that byte, in order to read or write the target bit.</p> Signup and view all the answers

In bit manipulation, registers are manipulated as collections of bits; common sizes include 8(bytes), 16, 32, and ______.

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

Consider the byte 0x3F. If you perform a left bit shift operation by 2 (0x3F << 2), what is the resulting byte?

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

When toggling a bit in a byte, the XOR (^) operation always sets the bit to 1 regardless of its initial state.

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

If sizeof(int) is 4 bytes, how many bits does that store?

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

Describe the purpose of applying bitmasks through bitwise operations in the context of bitstream or bitmap.

<p>Isolate or modify specific bits by masking out other bits.</p> Signup and view all the answers

Given the bitstream 01101001, applying a bitmask of 00000001 using the AND operation would ______ bit.

<p>isolate/extract/test/check</p> Signup and view all the answers

A bitstream is always stored as a contiguous sequence of bits, without any padding or alignment requirements.

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

In the function int test(char *bytes, int bit), what happens if you change int idx = bit / 8 to int idx = bit % 8?

<p>The function will compute the wrong byte index, leading to incorrect test/results.</p> Signup and view all the answers

Given char stream[] = {0xb6, 0x57, 0x1c, 0xd0};, finding bit 22 involves indexing element ______ of the stream.

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

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?

<p><code>color &amp; 0b10000000</code> (C)</p> Signup and view all the answers

When the problem specification doesn't direct you, adopting the endianness typical of the system ensures maximal portability.

<p>True (A)</p> Signup and view all the answers

Explain the difference between setting and toggling a bit.

<p>Set guarantees the bit will be a <code>1</code>; toggle flips from <code>1</code> to <code>0</code> and vice versa.</p> Signup and view all the answers

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.

<p>&lt;&lt;</p> Signup and view all the answers

An image is 300 pixels wide, represented as a bitstream where each pixel demands exactly one bit. How many bytes encapsulate one row?

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

Bitmaps offer lossless scaling because the vector-based formats store a continuous range of values across every zoom.

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

In C, explain what happens if you attempt to directly assign a byte (char myByte) the floating-point value of 3.1415.

<p>C will truncate the floating-point floating-point part of that value. Therefore, the <code>myByte</code> becomes <code>3</code> not <code>3.1415</code>.</p> Signup and view all the answers

Dr. Brodsky developed all slides unless ______ otherwise.

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

Flashcards

What is a bitstream?

A sequence of bits packaged into one or more bytes/words.

What is a bitmap?

Set of bits packaged into one or more bytes where each bit represents presence/absence.

What is a bitmask?

A byte/word applied via bitwise operations to extract or modify a single bit.

What is a positive bitmask?

A bitmask where a 1-bit indicates the bit to extract or modify.

Signup and view all the flashcards

What is a negative bitmask?

Bitmask where a 0-bit indicates the bit to be modified.

Signup and view all the flashcards

What does test(bits, i) do?

Returns the value of bit i of bits.

Signup and view all the flashcards

What does set(bits, i) do?

Changes bit i of bits to 1.

Signup and view all the flashcards

What does reset(bits, i) do?

Changes bit i of bits to 0.

Signup and view all the flashcards

What does toggle(bits, i) do?

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

Signup and view all the flashcards

What is the general structure of bit operations?

Compute the byte/word of the target bit, location, bitmask, and perform the bitmask operation.

Signup and view all the flashcards

Why determine the byte/word?

To access a specific spot in memory, select byte or 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, bit B is contained in byte B / 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 is 4 / (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, shift 1 left b times char bitmask = ~(1 << b);

Studying That Suits You

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

Quiz Team

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.

More Like This

Digital Systems and Binary Addition Quiz
5 questions
Binary data representation
11 questions

Binary data representation

AltruisticSwaneeWhistle avatar
AltruisticSwaneeWhistle
Bitwise Operators in Programming
22 questions
2.6
8 questions

2.6

MagnanimousCloisonnism avatar
MagnanimousCloisonnism
Use Quizgecko on...
Browser
Browser