Thread: bitwise OR of hexidecimal numbers in C

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    1

    bitwise OR of hexidecimal numbers in C

    how can we bitwise OR these Hex. numbers(unsigned integers) in C?

    0x00000001u
    0x00000010u
    0x00000020u
    0x00000200u
    0x00001000u
    0x00002000u
    0x00004000u
    0x00008000u

    i want to use the result in another language so please explain how to comment
    integer and unsigned integer because they confuse my mind for ex. 34 is integer and unsigned integer at the same time?

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by olegharput View Post
    how can we bitwise OR these Hex. numbers(unsigned integers) in C?

    0x00000001u
    0x00000010u
    0x00000020u
    0x00000200u
    0x00001000u
    0x00002000u
    0x00004000u
    0x00008000u

    i want to use the result in another language so please explain how to comment
    integer and unsigned integer because they confuse my mind for ex. 34 is integer and unsigned integer at the same time?

    It's quite common in C to need a fairly large number of boolean
    parameters. Instead of passing large numbers of parameters
    individually, it is no uncommon to wrap them up into a single
    integer, with each flag being a bit of that integer. So all the
    flags are powers of two.

    Note, I've called it an "integer", but it isn't really an integer. A real
    integer counts something, and these integers don't. They are just
    collections of bits, their value is arbitrary and of no meaning. To
    indicate that, the convention is to declare them "unsigned".
    Normally you have fewer than 32 flags and the top bit is always
    zero, so it's just a convention, it won't actually make any difference
    if you make your value a plain "int".

    The bitwise "or" operator is '|' (vertical bar). It's a single vertical
    bar. Normally you #define your flags. So you can construct a bitset with

    Code:
    /* all powers of two. It doesn't really matter which bits you choose */
    #define FLAG1 0x01
    #define FLAG2 0x02
    #define FLAG3 0x04
    
    unsigned int flags = FLAG1 | FLAG2 | FLAG3;
    Note the order of the flags doesn't matter.

    Now to test it we do the folowing

    Code:
      if (flag & FLAG1)
        /* execute code for flag 1 set */
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bitwise shift operators in -ve numbers
    By rohit_second in forum C Programming
    Replies: 11
    Last Post: 09-15-2008, 01:18 PM
  2. Conversion of hexidecimal to decimal
    By twocool82 in forum C Programming
    Replies: 6
    Last Post: 07-09-2007, 03:33 AM
  3. how to do conversion decimal to hexidecimal
    By twocool82 in forum C Programming
    Replies: 7
    Last Post: 07-08-2007, 06:20 PM
  4. Reading a file in hexidecimal
    By pinkcheese in forum C++ Programming
    Replies: 1
    Last Post: 03-15-2003, 06:06 AM
  5. Help with decimal to hexidecimal program
    By CheeseMonkey in forum C++ Programming
    Replies: 1
    Last Post: 12-22-2001, 03:15 PM

Tags for this Thread