0x0F stuff

This is a discussion on 0x0F stuff within the C++ Programming forums, part of the General Programming Boards category; Hello! I often do research and explore source files from other programs. More often, I see strange pieces of code, ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102

    0x0F stuff

    Hello! I often do research and explore source files from other programs. More often, I see strange pieces of code, like:
    Code:
    if ( (m5 & 0xF0) == 0) {
    		byte b = m5 & 0xF;
    I am really confused... my ambitions are to become a good programmer, so I have to know all about this... hex and binary stuff, if someone knows some good links to tutorials or articles regarding to this, please post them here. I wasn't born in the 70s, when you had to know only low-level programming languages, so I don't know them.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Most C programming books have a section on hexadecimal and bitwise operators.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    It is simple: & is the bitwise AND operator (do you know about boolean?). If m5 has a
    bit pattern of 00010100 in memory(2 powered 4 + 2 powered by 2) and 0xF=(15) has a
    bit pattern of 00001111 then b will have a
    bit pattern of 00000100 that is equal to 4.
    [edit]
    In this way we can ignore the left 4 bits of m5.
    Last edited by siavoshkc; 08-14-2006 at 04:32 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In other words, the resulting bit will only be 1 if both of the input bits are 1.

    You can find information like this in truth tables. They look like this:

    Code:
    AND (&)
    in1 in2 out
     0   0   0
     1   0   0
     0   1   0
     1   1   1
    
    OR (|)
    in1 in2 out
     0   0   0
     1   0   1
     0   1   1
     1   1   1
    
    ...
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    To convert hexadecimal to decimal:
    Hexadecimal digits are:
    1 2 ... 9 A=10 B=11 C=12 D=13 E=14 F=15
    When
    Code:
    4*3 = 3 multiplied by 4 = 12
    4^3 = 4 powered by 3 = 2^6 = 64
    (3*(5^2 )) = 5 powered by 2 then the result is multiplied by 3 = 3*25 = 75
    0 multiplied by any number is zero. So for example 0*(16^2) = 0
    Then we convert hexadecimal to decimal like this:
    Code:
    0x0024AD3= (3*(16^0))+ (13*(16^1))+(10*(16^2))+(4*(16^3))+(2*(16^4))+(0*(16^5))+...
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102
    Thanks, a bit clearer about the operators, but when do we use hexadecimal values and why? What's the purpose of them? Examples and links appreciated...

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Code:
    F = (2^3)+(2^2)+(2^1)+(2^0) =           1111
    E = (2^3)+(2^2)+(2^1) =                  1110
    D = (2^3)+(2^2)+(2^0) =                 1101
    C = (2^3)+(2^2) =                      1100
    B = (2^3)+(2^1)+(2^0) =             1011
    A = (2^3)+(2^1) =                           1010
    9 = (2^3)+(2^0) =                          1001
    8 = 2^3 =                                1000
    7 = (2^2)+(2^1)+(2^0) =             0111
    6 = (2^2)+(2^1)  =                0110
    5 = (2^2)+(2^0) =                  0101
    4 = (2^2) =                                0100
    3 = (2^2)+(2^0) =                          0011
    2 = (2^1) =                                      0010
    1 = (2^0) =                                      0001
    0 =                                            0000
    Now it is so easy to convert from hexadecimal to binary. Each hexadecimal digit represents four binary digits.
    Code:
    0xAA = 1010 1010 
    0xA4 = 1010 0100
    0xFD4A = 1111 1101 0100 1010
    0xABCDE = 1010 1011 1100 1101 1110
    Hexadecimal is easier to work with. Now try to write binary of 0xFF7028A4
    Last edited by siavoshkc; 08-15-2006 at 01:50 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Hex is useful because it maintains a closer relationship to binary than decimals,
    so it is very, very easy to convert a long hex number to binary (and vice versa).

    Example: if you were told to write this binary number in hex, you could do it in
    your head, whereas it'd be more difficult to convert to decimal:

    11101001101100101111

    Step 1: break the number into 4 bit pieces (if the number of bits isn't divisible
    by 4, add leading 0's - i.e. put extra 0's at the left of the number):

    1110 1001 1011 0010 1111

    Step 2: convert each 4 bit piece to hex (which is quite easy if you can count in
    binary already). Here's how you might appraoch it:

    1110 - this is 14 in decimal - which means it's E in hex (either look it up or you
    learn the hex digits off!)

    1001 - this is 9 in decimal - same in hex

    1011 - this becomes 11 in decimal, so it must be B

    0010 - 2 in decimal, 2 in hex

    1111 - this is 15 - hex has digits for numbers 0-15, so the last digit of the hex
    system is 15, which is F.

    Step 3: take the digits you just converted and string them together in the same
    order as they were in the first place - so the number is written simply as:

    E9B2F

    To convert from hex to binary, simply reverse the proceedure - write each hex
    digit as its binary equivalent and stick it together.

    There's also a number system called octal, which is base 8 - it has the same
    features as hex, only digits 0 - 7 are used so there's no need to worry about
    letters. You can do conversions with octal in the same way as hex, only you
    break the binary number into 3 bit pieces. There's a bad joke that goes as
    follows:

    Programmers (or some other binary educated group ), can't tell the difference between
    halloween and christmas because

    Dec 25 == Oct 31

    Convert octal 31 to decimal and you'll "get" it.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102
    Ah, cool, got it. Thank you very much for the brief explanation, now I understand. One last thing- how do I use hex in my program? I can cout<< them and use bitwise operations, but still, I don't get, where it is useful/needed to use hex instead of normal numbers. A simple example would be helpful.

  10. #10
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    For bit manipulation it is often easier to remember the hex (once you are used to it) for a bit than it is to remember the decimal for a bit... and it is easier to remember (i & 0xF) to get the lowest nibble than to remember (i & 15)... atleast for me it is.

  11. #11
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Great thing about binary, you only really need to know the first 15.

    Also notice this

    1 = 0001
    2 = 0010
    4 = 0100
    8 = 1000

    3 = 0011
    6 = 0110
    12 = 1100

    5 = 0101
    10 = 1010

    bit shifting left = multiplication by 2.

  12. #12
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    that's a nice trick! A lot easier to learn.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tab key stuff. C+WinAPI is killing me. Please help.
    By Templario in forum Windows Programming
    Replies: 5
    Last Post: 11-21-2002, 02:35 PM
  2. arguments, directories and stuff...
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-26-2002, 05:46 PM
  3. Your stuff
    By smog890 in forum C Programming
    Replies: 6
    Last Post: 06-13-2002, 11:50 PM
  4. Linked lists and file i/o, and some other stuff
    By ninja in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2002, 07:15 PM
  5. Stocks 'n' stuff...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-20-2001, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21