468,457 Members | 1,555 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

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

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 10551
weaknessforcats
9,207 Expert Mod 8TB
Hex value = 1b53, Binary= 0001 1011 0101 0011
To see of this bit is set:
Expand|Select|Wrap|Line Numbers
  1. 0001 1011 0101 0011
  2. .............^
  3.  
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
To see of this bit is set:
Expand|Select|Wrap|Line Numbers
  1. 0001 1011 0101 0011
  2. .............^
  3.  
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
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 <stdio.h>

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
weaknessforcats
9,207 Expert Mod 8TB
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
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
Ganon11
3,652 Expert 2GB
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
  1. :
  2.   10110010 11010101
  3. & 01001110 11010111
The result is 00000010 11010101. Do you see why this is the result?

How about this one:

Expand|Select|Wrap|Line Numbers
  1. :
  2.   10110101 00101100
  3. & 00000000 00000001
The result is 00000000 00000000. Do you see why this is the result?

Finally:
Expand|Select|Wrap|Line Numbers
  1. :
  2.   00011010 11100101
  3. & 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
weaknessforcats
9,207 Expert Mod 8TB
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
bfoo75
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
bfoo75
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
  1. bool result = i & mask;
  2. if (result)
  3.  cout << "1";
  4. else
  5.  cout << "0";
  6.  
shifting the mask looks like this:

Expand|Select|Wrap|Line Numbers
  1. mask = mask >> 1;
  2.  
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
Ganon11
3,652 Expert 2GB
shifting the mask looks like this:

Expand|Select|Wrap|Line Numbers
  1. mask = mask >> 1;
  2.  
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
  1. unsigned int i = 53; // 0011 0101
  2. i = i >> 1; // Shift all bits one position right
  3. // Now i is 0001 1010 (i think), which is 26, or about 53/2
  4.  
  5. unsigned int j = 83; // 0101 0011
  6. j = j << 1; // Shift all bits one position left
  7. // Now j is 1010 0110, which is 166, or 83 * 2
Feb 5 '08 #11
bfoo75
12
yeah your right. Sorry I don't work with bitwise ops very often.
Feb 6 '08 #12
Ganon11
3,652 Expert 2GB
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by ai lian | last post: by
8 posts views Thread by Michael A. Covington | last post: by
5 posts views Thread by dmitrey | last post: by
reply views Thread by castironpi | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.