How can i calculate the number and unsigned int with -1 is going to print
10 replies, posted
Hey.
So im currently learning for a Programming Test in my Highschool and on one Exercise it says " What do the following printf commands print out ?". Seems easy.
But then i noticed this printf("%d %u %X,-1,-1,-1);
Ive already checked what number -1 becomes when it gets printed using %u by tryping it into my c ide but i need to know how the HUGE number is being produced so i can calculate it when im wrinting that test.
Please help !
You need to know what the size is of an integer on the machine you are running the code on in bits / bytes.
In case it's 4 bytes (32 bits), the range of an unsigned integer is 0 to 2^32 - 1 where a signed integer is -2^16 to 2^16 - 1.
So, you can calculate it with the following formulas:
[code]
// Signed integers
minValue(x) = -2^(x/2)
maxValue(x) = (2^(x/2)) - 1
// Unsigned integers
// minValue is always 0
maxValue(x) = (2^(x)) - 1
[/code]
Where "x" is the size of the integer in bits.
[QUOTE=Cyberuben;49499993]You need to know what the size is of an integer on the machine you are running the code on in bits / bytes.
In case it's 4 bytes (32 bits), the range of an unsigned integer is 0 to 2^32 - 1 where a signed integer is -2^16 to 2^16 - 1.
So, you can calculate it with the following formulas:
[code]
// Signed integers
minValue(x) = -2^(x/2)
maxValue(x) = (2^(x/2)) - 1
// Unsigned integers
// minValue is always 0
maxValue(x) = (2^(x)) - 1
[/code]
Where "x" is the size of the integer in bits.[/QUOTE]
Thanks for that answer but how could i calculate that in my head? Im sorry if i that sounds stupid ( i am not ).
You should learn the powers of 2 until at least 2^256.
[QUOTE=MoustacheSpy;49500028]Thanks for that answer but how could i calculate that in my head? Im sorry if i that sounds stupid ( i am not ).[/QUOTE]
Write down all the powers of two. It's not all that difficult.
1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 etc. It's just the double of the previous. I don't think anyone will tell you it's illegal to write this down on a piece of paper. So, if you'd want to know what the range of a 32-bit signed integer is, you take 2^(32/2), which is 2^16 (which is 65536), then negate that, so you get -65536.
The maximum value is the same, but instead of negating, you substract 1, so, 65535.
[QUOTE=MoustacheSpy;49500028]Thanks for that answer but how could i calculate that in my head? Im sorry if i that sounds stupid ( i am not ).[/QUOTE]
The numbers are stored as binary numbers in the computer. So for a 32-bit computer, an int will thus be built up from 32 ones and zeroes. So the smallest number you can store with an unsigned int would be zero. If you try to subtract one now, it will "overflow" and become the biggest possible number an unsigned integer can store, which would be (2^(32)) - 1! (You have to subtract one because we start counting at 0, which is the first representable number.)
Maybe these pages will help you:
[url]https://en.wikipedia.org/wiki/Integer_overflow[/url]
[url]https://en.wikipedia.org/wiki/Integer_%28computer_science%29[/url]
(Also check out the "See Also" links at the bottom!)
[QUOTE=Darkwater124;49500045]You should learn the powers of 2 until at least 2^256.[/QUOTE]
Jesus christ, I'm clearly behind... I only really remember up to 2^16. I'll pretty sure only a savant autistic could remember all the way up to 2^256.
[QUOTE=Tommyx50;49502204]Jesus christ, I'm clearly behind... I only really remember up to 2^16. I'll pretty sure only a savant autistic could remember all the way up to 2^256.[/QUOTE]
He's not being serious with the 2^256, at least, I'd hope. I know them up to 2^32 with a bit of effort, but that's about it
[QUOTE=MoustacheSpy;49499880]Hey.
So im currently learning for a Programming Test in my Highschool and on one Exercise it says " What do the following printf commands print out ?". Seems easy.
But then i noticed this printf("%d %u %X,-1,-1,-1);
Ive already checked what number -1 becomes when it gets printed using %u by tryping it into my c ide but i need to know how the HUGE number is being produced so i can calculate it when im wrinting that test.
Please help ![/QUOTE]
If I'm not retarded and it's 32 bits ( which it most likely is ),
-1
UINT32_MAX - 2^32
and
0xFFFFFFFF ( 4 bytes, 8 nibbles )
[QUOTE=Trebgarta;49502283]... You could name it better than a savant autistic.
But im p sure nobody memorizes it all. Just know 2^32. Which is 32 bits.
[editline]11th January 2016[/editline]
Or yeah write 0xFFFFFFFF instead of being a savant autistic[/QUOTE]
0xFFFFFFFF is -1 :)
You're thinking of 0x7FFFFFFF
(please don't get mad and start shouting about unsigned numbers)
Sorry, you need to Log In to post a reply to this thread.