1. ## 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. >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. 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. >How did you know that 0xABC gives a decimal of 2748?? How did you work it out??
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. 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....

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. Hexadecimal is used because it is easy to convert into binary for humans.
101010111100 = 0xABC 7. 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 