C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 12th, 2002, 05:40 PM
Optix Optix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 36 Optix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
Unhappy 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.

Reply With Quote
  #2  
Old July 12th, 2002, 06:37 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 29 m 56 sec
Reputation Power: 4080
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.

Reply With Quote
  #3  
Old July 12th, 2002, 06:38 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
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.

Reply With Quote
  #4  
Old July 12th, 2002, 07:39 PM
Optix Optix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 36 Optix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old July 12th, 2002, 07:51 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 29 m 56 sec
Reputation Power: 4080
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.

Reply With Quote
  #6  
Old July 12th, 2002, 10:35 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed God (5000 - 5499 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 5,163 Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 6 Days 1 h 34 m 20 sec
Reputation Power: 791
Here is a good article on bitwise operations in C: http://www.gamedev.net/reference/ar...article1563.asp

Reply With Quote
  #7  
Old July 13th, 2002, 01:29 AM
Optix Optix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 36 Optix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
cool thanks ever1

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Bitwise Operators

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap