473,394 Members | 2,100 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,394 software developers and data experts.

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

Hi I am having trouble with my homework, i just started c language and this is my fourth week.

Homework Assignment: Week 4
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


so far this is what i have

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     int i, count, mask;
  7.     //   1B53  0001 1011 0101 0011
  8.     //   8000  1000 0000 0000 0000
  9.     i = 0x1b53;
  10.     mask = 0x8000;
  11.  
  12.     printf("i = %X, mask = %x\n", i, mask);
  13.     printf("i binary = 0001 1011 0101 0011\n mask binary = 1000 0000 0000 0000");
  14.  
  15.     for(i=0; i<=15; i++);
  16.     {
  17.         if(i & mask);
  18.         {//true
  19.         printf("1");
  20.         }
  21.             else;
  22.         {//false
  23.         printf("0");
  24.         }
  25.  
  26.     mask = mask >> 1;
  27.     }
  28.  
  29.  
  30.     return 0;
  31. }
it is telling me that there is an "else" without previous "if", also is the program that I wrote correct?
Thanks
Sep 22 '13 #1
3 4952
weaknessforcats
9,208 Expert Mod 8TB
It's here:

Expand|Select|Wrap|Line Numbers
  1. if(i & mask); <---!!!
  2.  {//true
  3.  printf("1");
Sep 22 '13 #2
Thanks, now it works. Is the program I wrote correct? when i run it just gives me a the i value, the mask value and a 0. I don't know if the loop is correct.
Sep 22 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
Just calculate the binary by and for i. So if i is 5 the binary is 101.

You are using an int which allows negative values so be aware that negative values are probably stored in 2's complement. Personally, I would use an unsigned ints.
Sep 23 '13 #4

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
12
by: waterdriven | last post by:
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.
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.