Bit Masking: How to Manipulate Bits in Binary Numbers

In summary: Thank you for explaining it to me.In summary, bit masking is a technique used to access specific bits in a byte of data. To turn off certain bits, a mask is used to set those bits to 0 while leaving the rest untouched. To toggle the values of certain bits, a mask is used to perform a bitwise xor operation with the input number. In both cases, the input number must have enough bits to accommodate the mask, with 2 bits on each side to be left unchanged.
  • #1
gEOdude
25
0
Hey Guys, can i have a hand with this question. I just want to know if what I did was right.

Bit masking has a task. They are used in order to access specific bits in a byte of data.

1) Using the 8-bit binary number “1001 1101”:
Turn off the 2 bits (masking bits to 0) on each side, and then leave the middle 4 alone.The output should be achieved should be "0001 1100”.

2) Using the 8-bit binary number “1001 1101”:
Toggle the values of the middle 4 bits (the opposite of what it currently is), and leave the 2 bits on each side untouched.The output achieved should be"10100001”.
Answer
1) (1001 1101) & (0101 1110) = 0001 1100
2) (1001 1101) ^ (0111 1110) = 1010 0001
 
Technology news on Phys.org
  • #2
HELPMEHELPME said:
1) Using the 8-bit binary number “1001 1101”:
Turn off the 2 bits (masking bits to 0) on each side, and then leave the middle 4
alone.

HELPMEHELPME said:
Toggle the values of the middle 4 bits (the opposite of what it currently is), and leave the 2 bits on each side untouched.
Why do these quotes talk about the middle 4 bits and the 2 bits on each side, that is, 6 bits in total, when the input number consists of 8 bits?
 
  • #3
Im not sure, its how my teacher wrote the problem. Is there something wrong with it?
 
  • #4
HELPMEHELPME said:
Bit masking has a task. They are used in order to access specific bits in a byte of data.

1) Using the 8-bit binary number “1001 1101”:
Turn off the 2 bits (masking bits to 0) on each side, and then leave the middle 4 alone.The output should be achieved should be "0001 1100”.

The mask that masks out 2 bits on each side is [m]0011 1100[/m].
Using this mask we can do:
1001 1101 & [m]0011 1100[/m] = 0001 1100

2) Using the 8-bit binary number “1001 1101”:
Toggle the values of the middle 4 bits (the opposite of what it currently is), and leave the 2 bits on each side untouched.The output achieved should be"10100001”.

Using the same mask:
1001 1101 ^ [m]0011 1100[/m] = 1010 0001
Answer
2) (1001 1101) ^ (0111 1110) = 1010 0001

I'm afraid the xor operator is not evaluated properly here. (Worried)

Evgeny.Makarov said:
Why do these quotes talk about the middle 4 bits and the 2 bits on each side, that is, 6 bits in total, when the input number consists of 8 bits?
HELPMEHELPME said:
Im not sure, its how my teacher wrote the problem. Is there something wrong with it?
No.
2 bits on each side means 2 bits to the left and 2 bits to the right, for 2+4+2=8 bits in total.
 
  • #5
I like Serena said:
2 bits on each side means 2 bits to the left and 2 bits to the right, for 2+4+2=8 bits in total.
Of course. Sorry about my misunderstanding.
 

FAQ: Bit Masking: How to Manipulate Bits in Binary Numbers

What is bit masking?

Bit masking is a technique used in computer science and mathematics to manipulate individual bits or groups of bits in a binary number. It involves using logical operators such as AND, OR, and XOR to set, clear, or toggle specific bits in a binary number.

Why is bit masking useful?

Bit masking is useful for a variety of reasons, including data compression, encryption, and optimization of memory usage. It allows for efficient storage and manipulation of data, especially in situations where memory is limited or when working with large data sets.

How do you perform bit masking?

To perform bit masking, you need to understand the binary representation of numbers and the different logical operators such as AND, OR, and XOR. Then, using these operators, you can specify which bits you want to manipulate and what action you want to perform on them.

Can bit masking be used for both setting and clearing bits?

Yes, bit masking can be used for both setting and clearing bits. For example, you can use the AND operator to set a specific bit to 1 or clear it to 0, depending on the bitmask you use. Similarly, you can use the OR operator to set a bit to 0 or clear it to 1.

Are there any risks associated with bit masking?

Bit masking can be a powerful tool when used correctly, but it can also introduce errors if not used carefully. It's important to understand the binary representation of numbers and how different logical operators work to avoid unintended consequences. Additionally, bit masking can make code less readable, so it's important to use comments and clear variable names to make it easier for others to understand your code.

Similar threads

Replies
19
Views
3K
Replies
11
Views
1K
Replies
2
Views
11K
Replies
2
Views
13K
Replies
8
Views
2K
Replies
2
Views
2K
Replies
34
Views
21K
Back
Top