Bitwise Operators, Help!!

This is a discussion on Bitwise Operators, Help!! within the C Programming forums, part of the General Programming Boards category; Hi I'm reading Ivor Horton's Beginning C, Third edition. And it came to Bitwise operations where I didn't quit understand ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    2

    Bitwise Operators, Help!!

    Hi I'm reading Ivor Horton's Beginning C, Third edition. And it came to Bitwise operations where I didn't quit understand this:

    unsigned int original = 0xABC

    what does it do, and how??
    here is the rest of the source code if you what to have a look.


    /* Program 3.10 Exercising bitwise operators */
    #include <stdio.h>

    void main()
    {
    unsigned int original = 0xABC;
    unsigned int result = 0;
    unsigned int mask = 0xF; /* Rightmost four bits */

    printf("\n original = %X", original);

    /* Insert first digit in result */
    result |= original&mask; /* Put right 4 bits from original in result */

    /* Get second digit */
    original >>= 4; /* Shift original right four positions */
    result <<= 4; /* Make room for next digit */
    result |= original&mask; /* Put right 4 bits from original in result */

    /* Get third digit */
    original >>= 4; /* Shift original right four positions */
    result <<= 4; /* Make room for next digit */
    result |= original&mask; /* Put right 4 bits from original in result */
    printf("\t result = %X\n", result);
    }

    Thank you

  2. #2
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >unsigned int original = 0xABC
    I think this is the first time someone has asked about the bitwise operators and not mentioned them in the question itself. The declaration is simple: original is an integer that can't be negative and it's assigned the hexadecimal value 0xABC, or 2748 in decimal.

    >void main()
    Regardless of what your book may say, this is wrong. main always returns an integer:
    Code:
    int main(void)
    As for what the rest of the code does and how, you should run it to see what it does and then use your books explanations of how the bitwise operators work to figure out how. That's far more instructive than us simply telling you what's going on.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    2
    How did you know that 0xABC gives a decimal of 2748?? How did you work it out??

    And regard to void main() is wrong, and that it returns an integer, why is it wrong??

    I'm sorry if I'm being a bit silly asking all these questions

  4. #4
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >How did you know that 0xABC gives a decimal of 2748?? How did you work it out??
    http://www.permadi.com/tutorial/numHexToDec/
    Or instead of doing it by hand you could use a calculator that does it for you. I think the Windows calculator in scientific mode will let you enter and convert values in binary, octal, hexadecimal, and decimal.

    >why is it wrong??
    The quick answer is that the C standard says so. I usually put more weight on what the standard says than a beginner's book simply because it's far more precise about what's valid and what's not.

  5. #5
    .
    Join Date
    Nov 2003
    Posts
    307
    Quote Originally Posted by Mini__C
    How did you know that 0xABC gives a decimal of 2748?? How did you work it out??

    And regard to void main() is wrong, and that it returns an integer, why is it wrong??

    I'm sorry if I'm being a bit silly asking all these questions
    1. 0xABC is a hexadecimal (base 16) number. The form 0x....
    says to the compiler this is hexadecimal. Try reading about hexadecimal numbers and then you can work it out, too.

    2. Amazon lists the third edition as published in April 2004.
    I'm surprised that "void main" would still be around in modern text books. Read the FAQ for this Board.

    It discusses main().

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Hexadecimal is used because it is easy to convert into binary for humans.
    101010111100 = 0xABC
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by jim mcnamara

    2. Amazon lists the third edition as published in April 2004.
    I'm surprised that "void main" would still be around in modern text books.
    I live just down the street from a big book store, so I will occasionally pop in to look at the programming books - Most of the time when I look at the C books, the author has used void main() - old habits die hard I guess.

    ~/

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, 09:43 PM
  2. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 12:37 PM
  3. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 03: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
    By Qasim in forum C Programming
    Replies: 1
    Last Post: 08-31-2001, 10:23 AM

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