The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Bitwise Operators
Discuss Bitwise Operators in the C Programming forum on Dev Shed. Bitwise Operators C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 12th, 2002, 05:40 PM
|
|
Contributing User
|
|
Join Date: Jun 2002
Posts: 36
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
Bitwise Operators
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.
|

July 12th, 2002, 06:37 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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.
|

July 12th, 2002, 06:38 PM
|
|
Contributing User
|
|
Join Date: Jan 2002
Location: Seattle WA
Posts: 863
  
Time spent in forums: 22 sec
Reputation Power: 13
|
|
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.
|

July 12th, 2002, 07:39 PM
|
|
Contributing User
|
|
Join Date: Jun 2002
Posts: 36
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
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
|

July 12th, 2002, 07:51 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
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.
|

July 12th, 2002, 10:35 PM
|
 |
/(bb|[^b]{2})/
|
|
Join Date: Nov 2001
Location: Somewhere in the great unknown
|
|
|

July 13th, 2002, 01:29 AM
|
|
Contributing User
|
|
Join Date: Jun 2002
Posts: 36
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
|
cool thanks ever1
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|