| | |
Enquiries about Bitwise operators
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 39
Reputation:
Solved Threads: 0
Dear experts,
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
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
•
•
•
•
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.
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!)
Last edited by tux4life; Aug 22nd, 2009 at 11:21 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
Then what does this line do?
set the mask to 0xF7 and peform the bitwise AND operation with the variable.
What is the meaning of the 0xF7?
0xF7 is a hexadecimal number, it is the same as 247 in decimal.But in C/C++ you don't have to convert it or something, you just set your mask to
0xF7 .You'll probably say: How do I do that?
Well, it's not difficult:
C++ Syntax (Toggle Plain Text)
int mask; mask = 0xF7; /* set the mask */
Last edited by tux4life; Aug 23rd, 2009 at 12:38 pm. Reason: add info
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
Do we need to convert the hex to normal decimal and then based on the decimal and find out how to do it?
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.
Last edited by tux4life; Aug 24th, 2009 at 4:52 am. Reason: add info
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Aug 2009
Posts: 39
Reputation:
Solved Threads: 0
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?*/
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?
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):
C++ Syntax (Toggle Plain Text)
int var; var = 16; // assign the decimal number 16 to the variable
0x , an example of this: C++ Syntax (Toggle Plain Text)
int var; var = 0xF; // assign the hexadecimal number F to the variable
(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?*/
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).
Last edited by tux4life; Aug 25th, 2009 at 9:38 am. Reason: fix some grammar and typos; extend post with extra info
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Since it seems to be lingering:
16 decimal is 10 hex, which is 10000 in binary; 15 decimal is 0F hex, which is 1111 in binary. "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Question Regarding "Bitwise Operators in C" (C)
- Diff b/w logical operators and bitwise operators...? (C)
- Bitwise: Convert Primitives (Java)
- Understanding bitwise (C++)
- Shift Operators: >> and << (C++)
- Bitwise operators/ undefined and implementation-defined? (C++)
- Problems with bitwise operators (C)
- bitwise operations and masking (C)
- Bitwise operators (C)
Other Threads in the C++ Forum
- Previous Thread: Retrieve motherboard serialnumber on Windows 64 bit OS
- Next Thread: Decimal to HexaDecimal
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






