435,512 Members | 3,689 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

Writing a program to print out the binary value of a 16 bit number

 P: 4 Hello; I am a newbie. A homework assignment was assigned and I am having trouble getting started. The assignment reads: Write a program to print out the binary value of a 16 bit number. Create integers i, count, and mask. Set 'i' to a hex value of 0x1b53. Set mask to a value of 0x8000. Why? print a line to show the hex value of i and then the leader for the binary value like this: Hex value = 1b53 Binary= Use a for loop to loop 16 times and print 16 digits, using count as the loop counter To test for each digit value, bitwise and 'i' with 'mask' when the result for the bitwise and is true, print the number '1' when the result for the bitwise and is false, print the number '0' then shift mask one place to the right print a new line and then quit Use prtscrn and make a hard copy of the code with the console output. Extra: use the modulus of count and print a space after every 4th digit to make the binary easier to read The output should look like this: Hex value = 1b53, Binary= 0001 1011 0101 0011 Any suggestions? Feb 3 '08 #1
12 Replies

 Expert Mod 5K+ P: 9,197 Hex value = 1b53, Binary= 0001 1011 0101 0011 To see of this bit is set: Expand|Select|Wrap|Line Numbers 0001 1011 0101 0011 .............^   which is bit 4 and represents a value of 16, divide the number 1b53 by 16 and check the reminder. If it is 0, the bit is off. If not the bit is on. That's enough of a hint. Feb 3 '08 #2

 P: 4 To see of this bit is set: Expand|Select|Wrap|Line Numbers 0001 1011 0101 0011 .............^   which is bit 4 and represents a value of 16, divide the number 1b53 by 16 and check the reminder. If it is 0, the bit is off. If not the bit is on. That's enough of a hint. Well, maybe the hint is enough for a person familiar with programing... I have never taken any programming classes. We are in our 4th week of C. Well, thanks for the quick reply. Feb 3 '08 #3

 P: 4 Well, maybe the hint is enough for a person familiar with programing... I have never taken any programming classes. We are in our 4th week of C. Well, thanks for the quick reply. OK, this is what I have so far.... I am lost. #include int main () { int i, count, mask; mask=0x8000; i=0x1b53; printf("Hex value = %d, Binary = %d\n", i, mask); for (i = 0; i <= 15; i = i + 1) printf("1"); printf("0"); return 0; } Feb 3 '08 #4

 Expert Mod 5K+ P: 9,197 OK, more of a hint. Binary 16 is 10000. 16/16 is 1. Look at the remainder as a binary digit, either 0 or 1. You use the modulus operator for this. The remainder 1 % 2 is 1. The bit is set. Try it with this: 100000. This is 32. 32/16 = 2 and 2 % 2 is 0. Therefore the bit representing 16 is off. Just apply these rules to your code. Feb 3 '08 #5

 P: 4 OK, more of a hint. Binary 16 is 10000. 16/16 is 1. Look at the remainder as a binary digit, either 0 or 1. You use the modulus operator for this. The remainder 1 % 2 is 1. The bit is set. Try it with this: 100000. This is 32. 32/16 = 2 and 2 % 2 is 0. Therefore the bit representing 16 is off. Just apply these rules to your code. Thanks for your efforts. However, while you are describing the modulus operator, I am still trying to figure out how the bitwise & is supposed to work out in this thing. I am not a young person, this stuff is quite new to me, I will keep reading and maybe it will make sense. Appreciate your assistance though. Feb 3 '08 #6

 Expert 2.5K+ P: 3,652 Your problem description from the first post seems to give very detailed instructions on how to complete the task. Do you know how the bitwise logic operators work? For instance, what is the result of the following bitwise comparison: Expand|Select|Wrap|Line Numbers :   10110010 11010101 & 01001110 11010111 The result is 00000010 11010101. Do you see why this is the result? How about this one: Expand|Select|Wrap|Line Numbers :   10110101 00101100 & 00000000 00000001 The result is 00000000 00000000. Do you see why this is the result? Finally: Expand|Select|Wrap|Line Numbers :   00011010 11100101 & 00000000 00000001 The result is 00000000 00000001. Do you see why this is the result? The last two examples will really help you to get your problem. Do you see what is happening when you use & with the binary value 1? What if it was 2 (00000000 00000010)? Four? Eight? Feb 3 '08 #7

 Expert Mod 5K+ P: 9,197 Thanks for your efforts. However, while you are describing the modulus operator, I am still trying to figure out how the bitwise & is supposed to work out in this thing. I am not a young person, this stuff is quite new to me, I will keep reading and maybe it will make sense. 1) I'm older than you are. 2) I'll bet I was older than you are now when started C and C++. 3) your original post was to print out the binary value of a 16 bit number 4) you do not use the bitwise AND for that 5) you use the modulus operator (%). Here's 5, which is binary 101 5/4 = 1 % 2 -> 1 5/2 = 2 % 2 -> 0 5/1 = 5 % 2 -> 1 There are the bits of 5: 101 Feb 4 '08 #8

 P: 12 I think we can all agree that age has nothing to do with programming or this problem. Besides that, it might be nice to keep the remarks to a minimum, I know how it feels to be new and post on forums and getting a response that seems hostile like this one would deter me contributing to a forum... especially since I am new. Sorry if this is offends you... I enjoy reading these forums and I'd prefer not to see this turn into another 50 post keyboard war. Feb 4 '08 #9

 P: 12 I think the problem you have is understanding this pseudo-code: To test for each digit value, bitwise and 'i' with 'mask' when the result for the bitwise and is true, print the number '1' when the result for the bitwise and is false, print the number '0' then shift mask one place to the right It took me a few reads to understand excactly what he is saying, but what he wants is you to use the '&' operator on i and the mask to get a boolean value... Expand|Select|Wrap|Line Numbers bool result = i & mask; if (result)  cout << "1"; else  cout << "0";   shifting the mask looks like this: Expand|Select|Wrap|Line Numbers mask = mask >> 1;   this moves all the binary digits one place to the right... effectively multiplying everything by 2 or 2^1 more accurately. Check out wikipedia's explination... you want the Shifts in C++ section and the AND sections: http://en.wikipedia.org/wiki/Bitwise_operation or something to that extent. Feb 4 '08 #10

 Expert 2.5K+ P: 3,652 shifting the mask looks like this: Expand|Select|Wrap|Line Numbers mask = mask >> 1;   this moves all the binary digits one place to the right... effectively multiplying everything by 2 or 2^1 more accurately. Actually, shifting to the left does not multiply by 2. Shifting to the left does. Check out this example: Expand|Select|Wrap|Line Numbers unsigned int i = 53; // 0011 0101 i = i >> 1; // Shift all bits one position right // Now i is 0001 1010 (i think), which is 26, or about 53/2   unsigned int j = 83; // 0101 0011 j = j << 1; // Shift all bits one position left // Now j is 1010 0110, which is 166, or 83 * 2 Feb 5 '08 #11

 P: 12 yeah your right. Sorry I don't work with bitwise ops very often. Feb 6 '08 #12

 Expert 2.5K+ P: 3,652 yeah your right. Sorry I don't work with bitwise ops very often. It's ok; no one's perfect. Except msquared, and even she segfaults every now and then. Feb 6 '08 #13 