473,385 Members | 1,356 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,385 software developers and data experts.

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

2
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i, count, mask;

i = 0xF5A2;
mask = 0x8000;
printf("Hex value= %x, Binary=", i, mask);

for(count=0;count<15;count++)
{
if(i & mask)
{
printf("1");

}
else
{
printf("0");
mask = mask >> 1;
}

}

return 0;
}
Feb 1 '17 #1
3 1103
zetlyn
2
what am i missing? every time i use the program i get a binary of 111111111111111
Feb 1 '17 #2
weaknessforcats
9,208 Expert Mod 8TB
Your mask is not shifting right unless you print a 0. Since you don't print 0, the mask never shifts.
Feb 2 '17 #3
donbock
2,426 Expert 2GB
Look at the first printf (the one that prints the hex value). You pass two arguments after the format string (i and mask), but the format string has only one conversion specifier. You might as well not pass mask.

The int type is allowed to be as small as 16 bits. If it is indeed 16 bits for your compiler, then odd things could happen when the most significant bit is set. You might want to declare i and mask as long or unsigned int to avoid inadvertently creating negative values. You will have to update the format string used to print i accordingly.

You should get in the habit of printing a newline at the end of every line to insure the output buffer is flushed. Refer to this earlier post for an explanation of buffered versus nonbuffered I/O.
Feb 2 '17 #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: Patrik Malmström | last post by:
How do I read, write a file binary? I want to open, say, file.exe read it in to the program, then write it out to file2.exe. Like file copy, anyone have a code sample?
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
3
by: velabrito | last post by:
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....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...

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.