Enquiries about Bitwise operators

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2009
Posts: 39
Reputation: D.JOHN is an unknown quantity at this point 
Solved Threads: 0
D.JOHN D.JOHN is offline Offline
Light Poster

Enquiries about Bitwise operators

 
0
  #1
Aug 22nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Enquiries about Bitwise operators

 
0
  #2
Aug 22nd, 2009
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.
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!)
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."
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 39
Reputation: D.JOHN is an unknown quantity at this point 
Solved Threads: 0
D.JOHN D.JOHN is offline Offline
Light Poster

Re: Enquiries about Bitwise operators

 
0
  #3
Aug 23rd, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Enquiries about Bitwise operators

 
0
  #4
Aug 23rd, 2009
Originally Posted by D.JOHN View Post
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:
  1. int mask;
  2. 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."
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 39
Reputation: D.JOHN is an unknown quantity at this point 
Solved Threads: 0
D.JOHN D.JOHN is offline Offline
Light Poster

Re: Enquiries about Bitwise operators

 
0
  #5
Aug 24th, 2009
Then what if I have something that got do do with the 0xE7 and then I want to change it to a mode where it will toggle 3 bits to from 0 to 1. Then how do you create out this function?

Do we need to convert the hex to normal decimal and then based on the decimal and find out how to do it?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Enquiries about Bitwise operators

 
0
  #6
Aug 24th, 2009
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.
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."
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 39
Reputation: D.JOHN is an unknown quantity at this point 
Solved Threads: 0
D.JOHN D.JOHN is offline Offline
Light Poster

Re: Enquiries about Bitwise operators

 
0
  #7
Aug 25th, 2009
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?*/
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Enquiries about Bitwise operators

 
0
  #8
Aug 25th, 2009
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):
  1. int var;
  2. 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:
  1. int var;
  2. var = 0xF; // assign the hexadecimal number F to the variable
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).
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."
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,361
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Enquiries about Bitwise operators

 
1
  #9
Aug 25th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Enquiries about Bitwise operators

 
0
  #10
Aug 25th, 2009
Quick fix for anyone who's reading this thread: when you read one of my previous posts, replace every 16 you encounter with a 15
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC