Reputation: 3
I've got school task but IDK How to design the code at all...
The function that it need to do is,
input 8 byte integer (I think it has to be unsigned long long type, cause input number has to be 0 or more)
divide input number to four 2 bytes and save it each variable. <<for example, input number is 18446744073709551615 then, it will be 1111111111111111111111111111111111111111111111111111111111111111 in binary. (it doesn't have to convert to binary, just show you how to divide) divide like this 1111111111111111 / 1111111111111111 / 1111111111111111 / 1111111111111111 in four part>>
and make each part to hexadecimal <like this, 1111111111111111 / 1111111111111111 / 1111111111111111 / 1111111111111111 -> ffff / ffff / ffff / ffff>
finally, make ffff / ffff / ffff / ffff to decimal number 65535 / 65535 / 65535 / 65535
Sorry, for my bad English but I need your help so bad.... ;( My level is quite low so when you explain I need some example codes to see and understand.
Thank you for reading my question!
Upvotes: 0
Views: 448
Reputation: 223484
Supposing a byte is the eight-bit byte vulgaris, an eight-byte integer is 64 bits. Then:
<inttypes.h>
and <stdint.h>
.x
using uint64_t
from <stdint.h>
.scanf("%" SCNu64, &x)
to read the number from input.scanf
. If it is not 1, for one number read, print an error message and exit the program.x
by 0, 16, 32, and 48 to move each part to the low bits.0xffffu
, or you can do it by assigning the value to a uint16_t
variable, which stores only 16 bits.uint16_t
variable, you can print them with printf("%" PRIu16, variable);
. If they are in some other variable or type of expression, you need a different conversion specifier, such as PRIu64
if you have a uint64_t
expression.Upvotes: 1