Thread: What's the use of bitwise operators?

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    6

    Question What's the use of bitwise operators?

    Hello, I'm a newbie in C programming. So mind if I ask the practical uses of the bitwise operators (in real world programming)?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Well, first off it only takes a bit to represent a boolean value, so it's fairly inefficient to actually use an entire int or char to represent a flag.

    Also, in data compression, it is neccisary to be able to handle data this isn't byte-sized, so you need bitwise operators for that too.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Sayeh
    Guest
    Bitwise operators, quite simply, are necessary if you want to work with the individual bits in any arbitrary variable.

    Without bitwise operators, how would I find out if the 5th bit in a long word was set to a 1 or 0? How would I know if a number was negative or positive? How would I be able to multiply or divide by powers of 2, quickly? How would I be able to work with individual pixels on the screen, or write a codec, or perform really cool graphical tricks like a Linear Bitshift Register?

    Trust us, they are essential to all things that a computer does.

    enjoy.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Hi!

    The bitwise operators are really nescessary for cryptography and data compression. You can encrypt the number 4 with the key 3 with the operation number xor key = 4 xor 3 = 7. You can then decrypt the result with the operation result xor key = 7 xor 3 = 4.

    Itīs only an example!!!

    klausi
    When I close my eyes nobody can see me...

  5. #5
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    There are real world reasons for using bitwise operators, such as converting ASCII to base64 encoding (which is used in MIME).

    Using a bitwise operator to see if a number is even or odd is much more efficient than using the modulus operator.
    Code:
    if ( number & 1 )
      printf( "odd\n" );
    else
      printf( "even\n");
    Bitwise operators can also be used to test if an ASCII character is upper or lower case, and set the character to upper or lower case without doing a function call (like toupper(), etc).
    Code:
    if ( some_char & 32 )
      printf("It's lowercase!\n" );
    else
      printf("It's uppercase!\n" );
    Last edited by Deckard; 01-12-2002 at 09:06 AM.
    Jason Deckard

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Another example of the need for bitwise operators:

    An electronic system consists of devices which send messages to each other. Usually a message is a bitstring, this is a row of bits. Each bit, or perhaps a group of bits, has a special meaning within the message.

    So both sending and receiving devices need to be able to process the individual bits.

    Note that it is also possible to use bitfields to process bits. A bitfield is a struct in which each element represents a bit.

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Well, if you are thinking at all about getting into Windows programming, then you are going to have to be pretty savvy with bitwise operations and the sort. For instance, using the bitwise OR operator '|' is used to set options and settings.

    Definitely necessary. If you don't understand them, don't go on. Ask for help here, and learn them well.
    1978 Silver Anniversary Corvette

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    6

    Talking Thanks!

    Gee... I check this thread from time to time and you guy sure have enlighten me with different uses of them (which I thought bitwise operators are cumbersome to learn ^_^).

    Are there anymore uses for bitwise operators, then?

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Every situation where you have to deal with bits, you'll need them. So the number of applications of bitwise operators is almost endless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators
    By gnewfenix in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 08:43 PM
  2. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  3. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 02:31 PM
  4. bitwise and arithmetic Operators
    By Whiteghost in forum C Programming
    Replies: 4
    Last Post: 12-28-2006, 02:13 PM
  5. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 04:20 PM