let an integer say, x=0xffffff0;

when i shift left 4 positions in it.it got negative. x=x<<1; doing this 4th time,number got negative. please tell why it is so.

x=0xffffff0;
That's 7 hex digits, so the full value of your int, in hex is
0F FF FF F0
shifted 4 left that becomes
FF FF FF 00
ints are stored as 1 sign bit (0 = positive, 1 = negative) and 31 bits of value.

Sorry, that's as clear as I can make it. Just read it slowly and check any words you don't understand by looking on Google or Wikipedia

## All 7 Replies

x=0xffffff0;
That's 7 hex digits, so the full value of your int, in hex is
0F FF FF F0
shifted 4 left that becomes
FF FF FF 00
ints are stored as 1 sign bit (0 = positive, 1 = negative) and 31 bits of value.
So on your 4th shift the first bit changes from 0 to 1 and the int gets interpreted as a negative number.

x=0xffffff0;
That's 7 hex digits, so the full value of your int, in hex is
0F FF FF F0
shifted 4 left that becomes
FF FF FF 00
ints are stored as 1 sign bit (0 = positive, 1 = negative) and 31 bits of value.
So on your 4th shift the first bit changes from 0 to 1 and the int gets interpreted as a negative number.

Sorry, that's as clear as I can make it. Just read it slowly and check any words you don't understand by looking on Google or Wikipedia

Sorry, that's as clear as I can make it. Just read it slowly and check any words you don't understand by looking on Google or Wikipedia

okay! please just explain me how is it 0F FF FF F0 ? why u include 0 at the starting of it? my exam is there please explain

if your exam is about to arrive, I doubt this is the first time you've had the option to study this material.
realise that an int is (normally portrayed as) decimal, that what you want is hexadecimal, take a look at the differences and see if you can find the values with the same values in decimal and hexadecimal for

0F FF FF F0

and for

FF FF FF 00

Sorry, that's as clear as I can make it. Just read it slowly and check any words you don't understand by looking on Google or Wikipedia

my second question to u is that "when i say int x=0xffffffff"(8 times f) then it prints only -1.okay why it is negative i know it. but why its value is only "1"? it should be greater than 1 means much greater than 1?

The value part of an int for a negative value is held in "two's compliment" form, in which ffffffff is -1 and 80000000 is the largest possible negative number. It's a bit like inverting all the 1s and 0s for negative numbers, because that makes it much easier to design the hardware for adding numbers. The full story is a bit more complicated, so here's an article that explains it in great detail
http://en.wikipedia.org/wiki/Two%27s_complement

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.