473,769 Members | 5,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bit Pattern Problem

fb
Hi Everyone. Thanks for the help with the qudratic equation problem...I
didn't think about actually doing the math...whoops. Anyway... I'm
having some trouble getting the following program to work. I want to
output a bit pattern from base 10 input. All I get is a zero after the
input...I've looked over the code but can't see the problem...any ideas?
/* display the bit pattern corresponding to
a signed decimal integer */

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

int main(void)
{
int a, b, m, count, nbits;
unsigned mask;

/* determine the word size in bits and set the initial mask */
nbits = 8 * sizeof(int);
m = 0x1 << (nbits - 1); /* Place 1 in leftmost position */

/* main loop */
do {
/* read a signed integer */
printf("\n\nEnt er an integer value (0 to stop): ", a);
scanf("%d", &a);

/* output the bit pattern */
mask = m;
for(count = 1; count <= nbits; count++); {
b = (a & mask) ? 1 : 0; /* set display bit on or off */
printf("%x", b); /* print display bit */
if (count % 4 == 0)
printf(" "); /* blank space after ever 4th digit */
mask >>= 1; /* shift mask 1 position to the right */
}
} while (a != 0);
return EXIT_SUCCESS; /* If it's there why not use it people! */
}

Nov 14 '05 #1
47 5289
fb wrote:
nbits = 8 * sizeof(int);


The error is on this line.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #2
fb


Derrick Coetzee wrote:
fb wrote:
nbits = 8 * sizeof(int);

The error is on this line.


right-o thanks.

Nov 14 '05 #3
Derrick Coetzee wrote:
fb wrote:
nbits = 8 * sizeof(int);

The error is on this line.


Oops, no it's not. I'm an idiot. The actual error is on this line:
for(count = 1; count <= nbits; count++); {


Watch out for those accidental semicolons! ^

--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #4
fb


Derrick Coetzee wrote:
Derrick Coetzee wrote:
fb wrote:
nbits = 8 * sizeof(int);


The error is on this line.

Oops, no it's not. I'm an idiot. The actual error is on this line:
> for(count = 1; count <= nbits; count++); {


Watch out for those accidental semicolons! ^

cool...ok. ty.

Nov 14 '05 #5
Mac
On Wed, 01 Sep 2004 22:35:48 -0400, Derrick Coetzee wrote:
fb wrote:
nbits = 8 * sizeof(int);


The error is on this line.


Well, the line shouldn't be written that way, but I doubt that is the
problem. If anything, it should be:

nbits = CHAR_BIT * sizeof (int);

--Mac
Nov 14 '05 #6
Mac
On Thu, 02 Sep 2004 01:21:47 +0000, fb wrote:
Hi Everyone. Thanks for the help with the qudratic equation problem...I
didn't think about actually doing the math...whoops. Anyway... I'm
having some trouble getting the following program to work. I want to
output a bit pattern from base 10 input. All I get is a zero after the
input...I've looked over the code but can't see the problem...any ideas?
/* display the bit pattern corresponding to
a signed decimal integer */

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

int main(void)
{
int a, b, m, count, nbits;
unsigned mask;

/* determine the word size in bits and set the initial mask */
nbits = 8 * sizeof(int);
m = 0x1 << (nbits - 1); /* Place 1 in leftmost position */

/* main loop */
do {
/* read a signed integer */
printf("\n\nEnt er an integer value (0 to stop): ", a);
scanf("%d", &a);

/* output the bit pattern */
mask = m;
for(count = 1; count <= nbits; count++); {
Someone else already pointed out that you didn't mean to put a semicolon
after your for loop. That collapses it to an empty loop.
b = (a & mask) ? 1 : 0; /* set display bit on or off */
printf("%x", b); /* print display bit */
if (count % 4 == 0)
printf(" "); /* blank space after ever 4th digit */
mask >>= 1; /* shift mask 1 position to the right */
I don't think the above line will do what you expect.

From the last public draft of c89:

3.3.7 Bitwise shift operators
[...]
The result of E1 >> E2 is E1 right-shifted E2 bit positions.
[...]
If E1 has a signed type and a negative
value, the resulting value is implementation-defined.

At the machine level, you sometimes see a shift with sign extension for
signed integers, and your implementation might use that type of shift
rather than a logic shift. What this means is that shifting a negative
number to the right may fill the higher order bits with 1's. The
documentation for your implementation should say what it does, though.

As far as I know, which may not be very far, every implementation will
probably either fill the high order bits with zeros or fill them with ones.
} } while (a != 0);
return EXIT_SUCCESS; /* If it's there why not use it people! */
}


--Mac

Nov 14 '05 #7
fb wrote:
.... snip ... I'm having some trouble getting the following program to work.
I want to output a bit pattern from base 10 input. All I get is
a zero after the input...I've looked over the code but can't see
the problem...any ideas?

/* display the bit pattern corresponding to
a signed decimal integer */

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

int main(void)
{
int a, b, m, count, nbits;
unsigned mask;

/* determine the word size in bits and set the initial mask */
nbits = 8 * sizeof(int);
You should #include <limits.h> and replace 8 by CHAR_BIT. Bytes
need not contain 8 bits.
m = 0x1 << (nbits - 1); /* Place 1 in leftmost position */
To do this m should be an unsigned int. Shifting into the sign
position results in undefined behavior. Unsigned ints don't have
a sign bit. Make all the variables unsigned.

/* main loop */
do {
/* read a signed integer */
printf("\n\nEnt er an integer value (0 to stop): ", a); ^^^
What is this? You also need a "fflush(stdin); " after this.
scanf("%d", &a);
Check the result from scanf, which should be 1. Maybe:
if (1 != scanf("%ud", &a)) break;

/* output the bit pattern */
mask = m;
for(count = 1; count <= nbits; count++); { ^
What's this semicolon for?
b = (a & mask) ? 1 : 0; /* set display bit on or off */
printf("%x", b); /* print display bit */
simpler: putc(b + '0', stdout);
if (count % 4 == 0)
printf(" "); /* blank space after ever 4th digit */
simpler: putc(' ', stdout);
mask >>= 1; /* shift mask 1 position to the right */
}
} while (a != 0);
return EXIT_SUCCESS; /* If it's there why not use it people! */
}

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #8
>> /* read a signed integer */
printf("\n\nEnt er an integer value (0 to stop): ", a);

^^^
What is this? You also need a "fflush(stdin); " after this.


No, you absolutely NEVER need a "fflush(stdin)" . If you absolutely
have to invoke undefined behavior, use fflush(void main()). It will
probably save you a lot of time by failing to compile.

Gordon L. Burditt
Nov 14 '05 #9

"CBFalconer " <cb********@yah oo.com> wrote in message
news:41******** *******@yahoo.c om...

/* main loop */
do {
/* read a signed integer */
printf("\n\nEnt er an integer value (0 to stop): ", a);

^^^
What is this? You also need a "fflush(stdin); " after this.


Make that:

fflush(stdout);

But you knew that. :-)

-Mike
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2420
by: Christian Stork | last post by:
Hello everybody, I am using Python to prototype a compression algorithm for tree-shaped data, e.g., XML data or abstract syntax trees. I use a variant of the visitor design pattern--called weaver/yarn pattern--in order to traverse the tree that is being compressed or decompressed. I do not know of any other interesting application of the weaver/yarn pattern, but it seems like a very suitable subject of study for different...
0
7047
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
17
6645
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products. So I shall have an Abstract Base called 'Car' implemented by Toyota, Ford, and Buick. Further I'd like to enable to client to say Car *factory;
3
3151
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This design pattern is very similar to the design pattern applied by the Assembly class. The twist is...
4
3611
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the "LOT:". Source text may contain multiple "SUB: ... LOT:" blocks. For example this is my source text:
22
4742
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer. Also describes implementation via ChangeManager (Mediator + Singleton) 2) Pattern hatching by John Vlissides Describes Observer's implementation via Visitor Design Pattern. 3) Design Patterns Explained by Alan Shalloway and James Trott
19
3178
by: konrad Krupa | last post by:
I'm not expert in Pattern Matching and it would take me a while to come up with the syntax for what I'm trying to do. I hope there are some experts that can help me. I'm trying to match /d/d/d/s/d/d in any text. There could be spaces in front or after the pattern (the nnn nn could be without spaces also) but it shouldn't pick it up in case like this 1234 56768
2
2994
by: =?Utf-8?B?QWFyb24=?= | last post by:
Hi, I'm having a tricky problem where I want to accept a regular expression pattern from user input but can't get teh escape characters to be prcoessed correctly. If I take the same pattern and declare it in code with a preceeding @ character it works fine. To get the pattern to work from teh suer all \ have to be escaped, e.g. instead of \d a user would have to enter \\d.
11
1718
by: George Sakkis | last post by:
I have a situation where one class can be customized with several orthogonal options. Currently this is implemented with (multiple) inheritance but this leads to combinatorial explosion of subclasses as more orthogonal features are added. Naturally, the decorator pattern comes to mind (not to be confused with the the Python meaning of the term "decorator"). However, there is a twist. In the standard decorator pattern, the decorator...
8
2777
by: Slaunger | last post by:
Hi all, I am a Python novice, and I have run into a problem in a project I am working on, which boils down to identifying the patterns in a sequence of integers, for example ..... 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0...
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9979
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3948
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.