472,959 Members | 1,593 Online

# 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.

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 11231
weaknessforcats
9,208 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 ()
{
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,208 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.

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?

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,208 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
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
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

### Similar topics

 3 by: Jaakko Holster | last post by: Lets run following code: ------- snip ------ class Perm { static \$read = bindec('001'); static \$write = bindec('010'); static \$delete = bindec('100'); } 4 by: ai lian | last post by: When I use printf to print a large double number, the result is not the same as the original input number. For example: double num=899999999999.894400; printf("%lf\n",num); The output is:... 7 by: Golan | last post by: Hi, I need to convert a Binary value to Decimal. I've been told that the value is an unsigned one. How can I do this? I use memcpy into an unsigned char variable, but when I print the value I got... 8 by: Michael A. Covington | last post by: Is there a way to make a C# program print the date on which it was compiled? Finding the file date of the executable is one way, but it's not foolproof. Thanks! 2 by: Steven | last post by: Hello, I want to modify an binary value of stationary location in file. For example, 100h=0F, i want to change to 100h=FF, how to do? Thank you very much 5 by: dmitrey | last post by: hi all, could you inform how to print binary number? I.e. something like print '%b' % my_number it would be nice would it print exactly 8 binary digits (0-1, with possible start from 0) ... 0 by: castironpi | last post by: On May 7, 3:31 pm, Mensanator { 2 by: DJRhino | last post by: Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing... 2 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central... 0 by: Aliciasmith | last post by: In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app... 0 by: tracyyun | last post by: Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to... 2 by: giovanniandrean | last post by: The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions... 0 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on... 3 by: nia12 | last post by: Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of... 2 by: GKJR | last post by: Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...