Enquiries about Bitwise operators

I am new to the subject of bitwise operator.
I know some of the basics like the gates and how the gates function eg. and gate, or gate, exclusive nor gates. And also how to change decimal numbers to binary numbers.

But I am not quite sure about the rest of changing parts and how did the 0x47 come from and how to use it.

So I need help from your to give me a basic education on how they are use and please start from the most fundamental discribtion to a more complicated term thanks.

I will be online frequently to check the post. Please help me out thanks.

An example to the question I don't know:

To turn bit 3 of a variable to 0. the correct way is to:

Ans: Not sure how to do but I think I know it involve some thing about Mask and flags.

Hence I hope that your can help me out by typing what your can know on this post and I will try my best to learn. Thanks:)

Since it seems to be lingering: [icode]16[/icode] decimal is [icode]10[/icode] hex, which is [icode]10000[/icode] in binary; [icode]15[/icode] decimal is [icode]0F[/icode] hex, which is [icode]1111[/icode] in binary. Read More

[QUOTE]But now I need to make use of this hex value to carry out operations.[/QUOTE] Not always necessary, but in this case it seems most appropriate.
Just take the way which is easiest for you, and in most cases, it's directly inputting the value.
(If the value is in another … Read More

[B]Bit Masking[/B]
Setting, clearing and inverting bits while leaving others bits unchanged using Bitwise operators.
By using a string with zeroes and ones in particular spots, you can set certain bits to 1 while leaving others unchanged. [icode]This procedure is comparable to how a painter masks areas that he does … Read More

[QUOTE]
About the part of trying to turn off the last bit, actually, I understand the part about the truth table but it is just that I am not certain that since they mention is
0 0 0 1 1 0 0 1
& 1 1 1 1 1 1 … Read More

Ans: Not sure how to do but I think I know it involve some thing about Mask and flags.

Short answer: take a value where all bits (except the third) are 1 (the third needs to be 0), this is your mask.
Then you (bitwise) AND it together with the value where you want to set the third bit to zero.

A more detailed explanation on how to 'build' the mask:
As you want to set the third bit to zero, we need to take a value where (in binary) only the third bit is set to one, and all the other bits are 0, there exists such a value, and in decimal it's: 4 (in binary: 0100).
Now you subtract this value from a value where all bits are set to one, you can get this value by just doing: ~0 .
So far your mask looks like: ~0-4 .

A very small description of the ~ operator:
The ~ operator is called the bitwise-NOT-operator, it's a unary operator (just like the logical-NOT-operator: ! ), all what it does is flipping/reversing the bits in a binary value, a one becomes a zero and a zero becomes a one.
So to resume, all what ~0 does is flipping the bits in the binary value of 0 to one, in this case you'll get a binary value where all bits are set to one.

Then you (bitwise) AND both the mask and the value together and boom, you get the value where the third bit is zero.
(Make sure you put the mask between parentheses!)

Do we need to convert the hex to normal decimal and then based on the decimal and find out how to do it?

Nope, a value in an integer variable is technically stored as a binary value.
For example: whether you assign 16 (in decimal), or 0xF (in hexadecimal) to an integer variable, the integer variable will have exactly the same value (1111).
When you display it using cout, then by default, that binary value will be displayed as a decimal value, but remember that that is only the way how it's displayed.

You just assign the hex-value to the integer variable, and adapt the way I described previously.

So for example I have a 16 (in decimal), I want to place it in the system so that it can calculate the result, so I need to convert it into 0xF (in hexadecimal) so that the system understand is 16(in decimal) and then when for instance I want bit 3 to toggle, it will convert this 16(in decimal) to binary number 1111. And then the third bit counting from right will be toggled and the result will become 1011?

Then what if from the previous question:

To turn bit 3 of a variable to 0. the correct way is to toggle the binary value of a decimal which has it's 3rd bit binary counting from the right to the left to be zero and the rest to be 1, so that when it toggles(because of the AND operation) the result will cause the bit 3 of the variable to multiply with flags(1111)and become 0?

1011*1111=1011(this is why 3 bit is 0.)

and as for the questions about setting the mask to 0xF7 and peform the bitwise AND operation with the variable.

int mask;
mask = 0xF7;/*This part i know it is about setting the mask to 0xF7, but what about the AND operation. How do you know whether it is using AND operation? Just declare in this way and the system will know it is AND operation?*/

So for example I have a 16 (in decimal), I want to place it in the system so that it can calculate the result, so I need to convert it into 0xF (in hexadecimal) so that the system understand is 16(in decimal) and then when for instance I want bit 3 to toggle, it will convert this 16(in decimal) to binary number 1111. And then the third bit counting from right will be toggled and the result will become 1011?

I guess you misunderstood my previous post, wait let me explain it again:
As you know, C++ is a programming language, to make it a bit more complicated: C++ offers support for variables.
When you want to assign a literal to a variable (for all practical purposes a literal just means: a number), you can either do one of the following:
C++ offers built-in support for 3 other bases (which are different from binary): decimal, hexadecimal and octal (I'm not going in depth on octal).
You can directly assign a value from either one of those bases to a variable, no matter from which base you choose to assign a value, first that value is automatically converted to binary.
To assign a decimal value to a variable you just write out the number (in the way you usually do):

int var;
var = 16; // assign the decimal number 16 to the variable

However, if you want to assign a hexadecimal number to a variable, you need to let your compiler know you're assigning a hexadecimal value to the variable (and not a decimal value), you can do this by preceding the number you want to treat as a hexadecimal value with: 0x , an example of this:

The key point is that which way you choose, the variable will always contain the same binary value (in this case that binary value would be: 1111).
(Remember: an integer variable holds a binary value (not a decimal or hexadecimal value), this is where you're probably confused with, only the way how the variable is displayed on the screen can differ, you can for example assign no-matter-what-value-in-decimal to a variable, and then print out the value of that variable as a hexadecimal number on the screen, the reverse is also possible: you can assign a hexadecimal value to a variable, and print it out as a decimal value on the screen).

As far as your concern about this:

int mask;
mask = 0xF7;/*This part i know it is about setting the mask to 0xF7, but what about the AND operation. How do you know whether it is using AND operation? Just declare in this way and the system will know it is AND operation?*/

Nope, you're just assigning a value to a variable, you're not doing any operations on it yet.
There's no such thing in C++ as: declare a variable for the operations you're going to do with it.
To operate on the data a variable contains, you use operators.
For example, when you assign a variable to a value, you operate on data, and you use the assignment operator = to assign the hexadecimal value F7 to the variable (which is of type integer, and named: 'mask').
The AND-operator has nothing to do with it for now (unless of course, you use it at the right side of the assignment operator).

Ok. For this part I get it. So inside the C++, they already contain for us the 3 types available for the user to key in, decimal, hex and octal.

Then when the user wanted to key in which value, they just do like normally how we declare a decimal. And then when operations are carried out, the system will convert this numbers into binary numbers and carry out operations.

But now I need to make use of this hex value to carry out operations.

About the hex to binary, decimal and octal conversion, I am clear about it. So maybe I think we can move on to the next part, the operations. If my concept above is right.

Not always necessary, but in this case it seems most appropriate.
Just take the way which is easiest for you, and in most cases, it's directly inputting the value.
(If the value is in another base than decimal. Octal or Hexadecimal, then don't bother converting it to decimal, but just directly key it in that base).
Practical for your assignments/questions this means that you just key the numbers in, in the same base as they're in your assignment.
It doesn't matter whether you mix them or not, remember: internally it's all binary.

By using a string with zeroes and ones in particular spots, you can set certain bits to 1 while leaving others unchanged. This procedure is comparable to how a painter masks areas that he does not want to be painted, using plastic or perhaps masking tape. Thus, the process is called masking. The string of digits used in the operation is called the bit mask, or more simply, just the mask.

inside the if operation, the 0x10, that 10 is it being coverted to BCD or binary? Because when 0x10 (hex) is being converted to binary, I am not so sure about the output since 10/16 will give you a remainder of 0 and continuous dividing only give me a 0.

Hence, I am not so clear how come the result is 00010000.<--which is more like the BCD of 10 to me.

(Q2)Quote:
"
This would result in the following calculation
Collapse

00110010 - b
& 00010000 - & 0x10
----------
00010000 - result

So we see that bit four is set.
"

How do they know that the bit 4 is set(I mean I know that the cout cause it to display the output bit 4 is set. But since it is able to display that line, it means that this 'if ( b & 0x10 )' is satisfied and hence how do we know wether it is satisfied a not? Is it that the 4th bit of the result is 0 and this 4th bit of the if condition is 0, hence they say that bit 4 is set?

To start off: 0x10 is the hexadecimal number for 16 (in decimal), put differently: 0x10 (hexadecimal) and 16 (decimal) have the same binary representation: 00010000 , as you can see in the binary representation, only the fourth bit is set to one, so if you bitwise AND this value (often called 'the (bit)mask') together with another value, you get either a value which converts to true (non-zero) or false (zero).

Examples:

In case the fourth bit is zero (I just took a random binary value where the fourth bit is set to zero):
11001011 (the fourth bit is marked in bold green)
If you bitwise AND this together with your mask, which was: 0x10 , or 00010000 in binary, this will happen:

As expected, this evaluates to a false value (zero).
Maybe interesting to mention: if you have a mask, and you're doing a bitwise AND operation, every bit which is zero in the mask, will be zero in the resulting value, if a bit in the mask is one, and the corresponding bit in the value is also one, then this bit will be also one in the resulting value, otherwise this bit will be zero in the resulting value.

In case the fourth bit is one (I just took a random binary value where the fourth bit is set to one):

So as you have mentioned, we just need to convert the hex to binary if we want to understand how it works out the result.

Yup! To understand how it works, you have to 'think binary' :)

So if I want to turn bit 3 of a variable to 0, so we can use AND operation.

Yes, as long as you choose a correct mask.

But in this question, the flags(also known as the random number) is not given. Neither is that mask(those hex decimals or octal or binary numbers). Then how do we know what number we need to use.

Well, say you want to turn off bit 3 of a certain value, then you first need to choose a mask (as you know you want to turn off a bit, you choose the bitwise AND).
So if you want to turn off the third bit, the only thing you have to do then is: choose a value wherein only the third bit is zero, use that value as your mask, bitwise AND both (your mask and the value) together, period.

Quote: " Well, say you want to turn off bit 3 of a certain value, then you first need to choose a mask (as you know you want to turn off a bit, you choose the bitwise AND).
So if you want to turn off the third bit, the only thing you have to do then is: choose a value wherein only the third bit is zero, use that value as your mask, bitwise AND both (your mask and the value) together, period. "

so in this case does this means, I will have to choose a value myself instead of them giving, and I make use of the value and multiply by itself. Then the 3rd bit of the output must bea 0.

Example: 11011111

value(also known as flag)=11011111;
mask=11011111;
cout<<output;

the output being shown on the screen will be 11011111 am i right?

In addition, this value can be any value as long as it has it's 3rd bit be 0 right? So that 0 AND 0 will give you a 0.

The << operator shifts its first operand left by a number of bits given by its second operand, filling in new 0 bits at the right. Similarly, the >> operator shifts its first operand right. If the first operand is unsigned, >> fills in 0 bits from the left, but if the first operand is signed, >> might fill in 1 bits if the high-order bit was already 1. (Uncertainty like this is one reason why it's usually a good idea to use all unsigned operands when working with the bitwise operators.) For example, 0x56 << 2 is 0x158:

0 1 0 1 0 1 1 0 << 2
-------------------
0 1 0 1 0 1 1 0 0 0

And 0x56 >> 1 is 0x2b:

0 1 0 1 0 1 1 0 >> 1
---------------
0 1 0 1 0 1 1

About the shift operator, the >> operator is sort of moving 2 bits towards the left, hence adding the bits into it and when we read the binary, it is 0101011000(we read from the right to the left so 1000 is 8, 0101 is 5, 0001 is 1, so answer is 158 in decimal)

About the shift operator, the << operator in the example, it is mentioned to shift towards the right 1 time. so the input number is 0 1 0 1 0 1 1 0 >> 1, hence the output is 1010110 (which if we convert it into decimal and i look from the right to the left, 0110 is 6, 0010 is 2) but the guide say is 0x2b and not 0x26.

(Q2) To add on to this, in this part,

This may be easier to see if we look at it in binary. If the DIRTY, RED, and SEASICK bits were already on, flags would be 0x19, and we'd have

0 0 0 1 1 0 0 1
& 1 1 1 1 1 1 1 0
---------------
0 0 0 1 1 0 0 0

are they trying to turn off the last bit?

(Q3) In their guide, the masks are given and also the conditions are also given
Example:

>are they trying to turn off the last bit?
Did you maybe skip the truth table for AND-operations at the bottom of one of my previous posts?
Should I repost it again, or just give you a link to it?
Just apply each time the bitwise AND operation to two corresponding bits, and write down the result.

About the part of trying to turn off the last bit, actually, I understand the part about the truth table but it is just that I am not certain that since they mention is
0 0 0 1 1 0 0 1
& 1 1 1 1 1 1 1 0
---------------
0 0 0 1 1 0 0 0
, therefore, the 11111110, the 0 at the last bit hence is it tellng us that they are trying to toggle the last bit? <--That is actually what I am asking

(Q2)

About the shift operator, the << operator in the example, is simply just to say add 0 at which side and how many zeros. But inside the guide(please take a look at: http://c-faq.com/~scs/cclass/int/sx4ab.html) the guide says that

and the output when being converted is 0x2b. But based on what I think is correct, the >> operator means adding a zero at the front of the input binary hence causing the binary output to be 00101011.

When being converted to hexadecimal output, the output should be 0x26 and not 0x2b.

So is it that I am wrong or the guide's answer is wrong?

(Q3) In their guide, the masks are given and the conditions are given but in the exam, would such a question come out but they only tell you the condition for example I want to toggle bit 3. But did not tell you what masks you should use(eg. 0xF7).

(Q2) You're not going to change the number of bits a value is stored in. So if a value has 8 bits, it has 8 bits.

0 1 0 1 0 1 1 0 >> 1
x 0 1 0 1 0 1 1

The top bit vacated by the "empty bit" (denoted temporarily here as x) becomes zero. The LSB "falls off". (Or, from the link: "For both of the shift operators, bits that scroll "off the end" are discarded; they don't wrap around.") So you get:

0 0 1 0 1 0 1 1

Nothing is "converted". Representations are for human consumption. Binary representation, hex representation, decimal representation, others -- they're for human readability and understanding, a convenience for you. 0010 in binary is 2 in hex and decimal. 1011 in binary is B in hex and 11 in decimal.

(Q3) Bit 3: xxxxXxxx If you want to toggle it, you XOR a 1 with that bit and 0 with all others. So you get 00001000 . On an exam, it would seem implicit that you were told both the operation and the mask.

The same things (both operation and mask) are implicit to "set bit X" or "clear bit Y".

About the part of trying to turn off the last bit, actually, I understand the part about the truth table but it is just that I am not certain that since they mention is
0 0 0 1 1 0 0 1
& 1 1 1 1 1 1 1 0
---------------
0 0 0 1 1 0 0 0
, therefore, the 11111110, the 0 at the last bit hence is it tellng us that they are trying to toggle the last bit? <--That is actually what I am asking

Yep, if you use bitwise AND, then only if the two corresponding bits are one, then the bit in the outcome is also one, otherwise it is always zero (you can derive this rule from that truth table).

and the output when being converted is 0x2b. But based on what I think is correct, the >> operator means adding a zero at the front of the input binary hence causing the binary output to be 00101011.

Well, whether you write 0[B]101011[/B] , or 00[B]101011[/B] , or [B]101011[/B] , it all means the same, so your answer would be correct too, but sometimes extra zeroes (at the beginning of the binary number) aren't written (because it does not change the number).

When being converted to hexadecimal output, the output should be 0x26 and not 0x2b.

So is it that I am wrong or the guide's answer is wrong?

I don't know how you converted it, but I guess you must have made a mistake somewhere while converting both.

(Q3) In their guide, the masks are given and the conditions are given but in the exam, would such a question come out but they only tell you the condition for example I want to toggle bit 3. But did not tell you what masks you should use(eg. 0xF7).

If this type of question indeed come out, how do you solve it?

Well, that depends on what you want to do with the third bit, if you want to turn it on, you'll have to use a different mask than in case you want to turn it off.
If you want to turn on a particular bit in a binary number, then you should use the bitwise OR operator |, on the other hand, if you want to turn off a particular bit, then you'll have to use the bitwise AND operator &.
You only have to choose your mask in such a way, that only and only the third bit is affected, so that the other bits stay the same.

(Q1) The part about toggling now I am cleare now. It uses the XOR(exclusive or operator) so that the two input must be different hence in order to have an output 1. If they are the same, they will show an output 0.

But in the exam, what if they give you 2 input, 0x19 and 0x2F and for example ask you to find toggle bit 4.

then after calculating, you found out that fourth bit cannot be toggled because 0 XOR 0 will give you a 0. Hence no toggling at all then what should you give as an answer?

(Q2)Now I understand about this part. Because I mistakenly converted the binary to decimal from the left to right which is supposed to be converted the other way. Henc, by adidng a zero in front, th answer would be 0x2B.

(Q3)Which means in the exam, they would give you the masks and the flags and will test your knowlegde on which condition use what operation right? Then is it possible if I am given only a mask and condition and then would ask you to get the answer? For example given that mask is 0xF7 and I want to toggle the 3 bit. Is there any solution if yes, please share with me thanks:)

then after calculating, you found out that fourth bit cannot be toggled because 0 XOR 0 will give you a 0. Hence no toggling at all then what should you give as an answer?

How do you mean: the bit cannot be toggled? By my means you can always toggle a bit: in case the bit is one, you toggle it, and it becomes a zero; in case the bit is zero, you toggle it, and it becomes one.

BTW, as mentioned in Dave Sinkula's post, you toggle a bit by XORing it with 1:

Bit 3: xxxxXxxx If you want to toggle it, you XOR a 1 with that bit and 0 with all others. So you get 00001000 .

BTW, Dave: I guess we should assume in this example that the small (lowercase) x' are a zero?

(Q1) So about the toggling part, in the exam, when they give you example 0xF7, is this a mask or a flag.

Then after that you know that the question wanted you to toggle bit 3. Then you just need to assure that the 3rd bit(counting from right to left) needs to change from a 1 to a zero. Hence you just create the flag to be 0000 0100 (the 3rd bit to be a 1) so that the 3rd bit of the mask can change from a 0 to a 1. Am I right?

I think I need some clarification between is the given 0xF7 is the mask or is it a flag.

(Q2)Bit 3: xxxxXxxx
If you want to toggle it, you XOR a 1 with that bit and 0 with all others. So you get 00001000 .

I think that you are trying to toggle bit 4 and all the x's are 0. Since you wanted to toggle bit 4, the large X, then u just put in the flag as 0000 1000, and this will cause the output to change from 0000 0000 to 0000 1000 (Note that the 4th bit has been toggled). Is this what you mean?

(Q3)Now what we have discussed so far is in counting and converting our hex into binary and count the answer by ourselves, hence if I want to use c++ to show out the answer how do we do that?

Example:

int flags,mask;
mask=0xF7;
mask=flags^mask;
cout<<mask;

am I right? In addition, in this case, can I say that since I didn't declare flags to be any value (unlike mask which i say mask=0xF7) can I say that the system will automatically take the flags as 0000 0000? And if that is the case, we would not be able to turn off a specific bit example I want bit 3 to toggle. Then my mask is given as 0xF7 but since I didn't declare my flags(it will be 0000 0000) then the output will be 1111 0111 and the 0xF7(1111 0111) then there is no change in the third bit.

It would be best if you could give me an example in c++ and explain the terms and how it works to me.Thanks.

It would be best if you could give me an example in c++ and explain the terms and how it works to me.Thanks.

I guess that it would be the best to just use Google to find yourself some examples, remember: Google is your friend!
I'm convinced there are plenty of examples on bitwise operators floating around on the internet, so you won't have any trouble finding them, you know what? You could maybe start your search on this forum (Daniweb has a powerful search feature which (if used correctly) is capable of revealing the most interesting things on Daniweb :P.