473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 11393
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 ()
{
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,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.

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

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

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 <mensana...@aol.comwrote: ) for a in range( 10 ) ] ) 00000000 00000001 00000010 00000011 00000100 00000101 00000110
5
by: zehra.mb | last post by:
Hi, I had written application for storing employee data in binary file and reading those data from binary file and display it in C language. But I face some issue with writing data to binary file....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.