Monday, December 12, 2011

C question, binary number, need help?

In a C program a variable is required to store the following 16 bit binary number: 1101011010110111





a). Write a variable definition suitable for this definition.


b). Write a C statement that stores this value in your variable.|||The binary number has 16 bits so an unsigned short will be able to hold it. To convert it to a hex number is the easiest: The rightmost 4 bits, 0111 = 7, the next group of four, 1011 is 11 which is B in hex, and so forth. Convert all groups of four bits in hex digits and assign to your unsigned short variable. You know how to tell the compile that you have a hex, as opposed to a base-10 constant.|||Well actually it depends on the machine, as different machines will allocate different sizes for ints, chars, etc.





On my machine, just running a quick check, a short is 2 bytes and so your value would fit.





I would convert the number to hex and then store it, because there is a standard way to enter hex but entering binary is largely compiler dependent, so try this:





short value = 0xD6B7;

No comments:

Post a Comment