|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Hello,
I am reading a book on C++ (Begining Visual C++ 6.0) And i came across a topic on bitwise operators. I was reading through it and was totally lost What exactly is this needed for? Why would i care if i had.... for example....Code:
..... char letter1 = 'A', letter2 = 'Z', result = 0; result = letter1 & letter2; ..... When this is compiled it says.... A Z @ Basicly the bits of A & Z would result in 0100 0000 but why would i care about that? anyhelp would be appreciated. Thanks. -Optix If yyou guys want to see what I am talking about then go to This link (.zip file) and download the tut. (its a small file) Its in Chapter 1 >> Variables and Casting >> The bitwise operators.. Thanks. |
|
#2
|
||||
|
||||
|
That is a bad example for usage of a bitwise operator, IMHO. A more useful use for the AND bitwise operator & would be to check if a particular bit (or bits) in a byte is set or not. Let's set a more real world example. In a *NIX system, file permissions are a set of bits that indicate what permissions (read, write, execute) are allowed for the owner, group and others respectively. So, let's say that for a file foo.pl, the permissions are as follows:
Owner can read, write and execute this file (bit pattern 111) Group can only read and execute this file (bit patter 101) Others can only read this file (bit pattern is 100) Then the entire file permissions would be something like this (in binary 111 101 100, which works out to 492 in decimal or 754 in octal) Now let's say you want to check if a user from the group has permission to execute this file or not, you need to check if the 4th bit from the right is set or not (irrespective of the values of the other bits). You can do this by using a bitwise and operation to mask off the remaining bits. 111 101 100 & 1 000 will give you the result 1 000, which tells you that the fourth bit is on. In actual C code, this would look something like this: Code:
file_perms = stat.st_mode;
// Now check if the fourth bit is set or not
if (file_perms & 0x8 == 0x8) { // 0x8 in hex is 1000 in binary
// we have the required permissions
}
Hope this helps. |
|
#3
|
|||
|
|||
|
Bit flags. It's common to store states or information in a byte (or series of bytes), where each state (on or off) if one bit. That is, a byte, 00000000, can store eight flags, with each zero or one signifying on or off states of each flag.
Then consider this: bit 3 (in an eight bit byte) has the state of the property in interest. How do we see if bit 3 is set? Code:
SomeNum & BitMask = TrueOrFalse? xxxxx1xx & 00000100 = 00000100 (true) xxxxx0xx & 00000100 = 00000000 (false) Where the x's can be either 1s or zeros. The case wher they had you using char's isn't that common an application, but I think they were just trying to drive the point across that these operators literally perform bit-for-bit operations, and don't care what the underlying type is. |
|
#4
|
|||
|
|||
|
Thanks guys that did help me out
But can i ask one more Q please K i was wondering where u can find hex/binary values. Like how scorpians said... "0x8 in hex is 1000 in binary"THank you! -Optix |
|
#5
|
||||
|
||||
|
Converting binary to hex is usually covered in most introduction to computers books, if you want to learn how to do it by hand. If you don't care to do it by hand, you can always use the Windows Calculator (convert it to Scientific mode first: View --> Scientific). Most scientific handheld calculators (available from $12 upwards) also have functionality to convert numbers between binary, octal, hex and decimal.
|
|
#6
|
||||
|
||||
|
Here is a good article on bitwise operations in C: http://www.gamedev.net/reference/ar...article1563.asp
|
|
#7
|
|||
|
|||
|
cool thanks ever1
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Bitwise Operators |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|