2

Hi can someone please help me understand how to set or rest certain bits. i believe it is known as masking but unsure how to proceed. I would be grateful for any help with my studies. The question is:

Given numbers a) 01111001 and b) 10001010.

  1. What 8 bit binary number would you use to RESET bits 6 and bits 0 of number a) and what logical operation would you use?

  2. What 8 bit binary number would you use to SET bits 5 and bits 2 of number b) and what logical operation would you use?

orlagh
  • 35
  • Are you familiar with XOR? – blub Aug 30 '18 at 12:56
  • What logical operations have you encountered so far, and what do they do to the bits of a binary number? – Jaap Scherphuis Aug 30 '18 at 13:05
  • Hi, yes i have encountered AND, OR, XOR. Previously i went over using logical operand one two binary numbers. i know they change the bits depending on the operand. i think i am more concerned on how it asks for certain bits to be set or reset. – orlagh Aug 30 '18 at 13:35

2 Answers2

0

I assume:

  • reset means to set a bit to $0$
  • set means to set a bit to $1$
  • numbers are little endian

To reset a bit, you have to use the $AND$ operation with a mask where the affected bit is set to $0$ and all other bits are set to $1$:

    01111001
AND 11011110
------------
    01011000

To set a bit, you have to use the $OR$ operation with a mask where the affected bit is set to $1$ and all other bits are set to $0$:

   10001010
OR 00010010
-----------
   10011010
0

Consider the XOR-operation, or $\hat{\lor}$, given over the following truth table for two binary variables:

$$\begin{array}{|c|c|c|c|} \hline p& q& p\hat{\lor}q\\ \hline 0& 0& 0\\ \hline 0& 1& 1\\ \hline 1& 0& 1\\ \hline 1& 1& 0\\ \hline \end{array}$$

You may generalize this operations for strings bit-wise. Can you apply this operation here? How would you set or reset a bit in a string? Play around with this a little(seriously, this is the key to getting an intuition) before you check the hint:

1. $10000010$ 2. $00100100$

EDIT: Since you've seen my answer, I want to add something regarding the answer of Stanley F., which I did not want to talk about before to not "spoil the surprise":

You may of course approach this problem with almost any classical binary operation(or at least with every set which is functionally complete). However, the advantage of using the exclusive or is of course that

  1. It applies to setting and removing bits.
  2. The to-be-modified bits correspond in their position to the set-ones in the corresponding string.
blub
  • 4,794
  • Thank you. i will play around with them. – orlagh Aug 30 '18 at 13:39
  • my confusion on the topic was partially due to the question asking to reset bits 0 as it is an 8 bit string i was counting from 1. so you can use the XOR operation to both set or reset? – orlagh Aug 30 '18 at 14:06
  • @orlagh Well consider the string 0101 just for simplicity. Suppose you want to set bit no. 2 using xor. Then, you may proceed by $0101\hat{\lor}0010=0111$. Similarly, to remove bit no. 3, you may proceed by $0101\hat{\lor}0001=0100$. This duality comes from the symmetry in the truth table. Note: of course $\hat{\lor}$ is here considered the expanded operation on $\mathbb{F}_2^n$, not the digit operation from the answer. – blub Aug 30 '18 at 14:16
  • thank you its quite clear. When asked to reset bit number 0. which bit is this referring to, the first? – orlagh Aug 30 '18 at 14:37
  • @orlagh It is common practice in computer science (and depending on who you ask, in mathematics as well) to start counting at $0$, so yes it means the first. – blub Aug 30 '18 at 14:39
  • thank you. your responses have helped a lot. – orlagh Aug 30 '18 at 14:44
  • hi, i have been playing around with logical operators. can i ask which one you used for your hint? – orlagh Aug 30 '18 at 15:06
  • @orlagh XOR as well – blub Aug 30 '18 at 15:09
  • Even though, I agree with the advantages you outlined, a big disadvantage of this approach is that you have to know the previous state of the bit, which requires one more step. With my approach, you just apply the mask, regardless of the bit's value. – Stanley F. Aug 31 '18 at 03:23
  • @StanleyF. Ah I did not think about, we should add this to our answers. – blub Aug 31 '18 at 10:07