473,750 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about bits (debugging>

I am working on a graphics program but my question has nothing to do
with graphics but trying to get an algorithm to work. I set graphics
from a 16x16 grid to bits of a graphic with:

bitData[index++] = binTemp[0] * 128 + binTemp[1] * 64 +
binTemp[2] * 32 + binTemp[3] * 16 +
binTemp[4] * 8 + binTemp[5] * 4 +
binTemp[6] * 2 + binTemp[7];

But I want to populate that same pad from bits:
void populate(int num){
int bits[8] = {0,0,0,0,0,0,0, 0};
int counter = 0;
int placer = 0;
BYTE temp;
clear();
std::vector<BYT Evb = work.getBits();
std::vector<BYT E>::const_itera tor itr = vb.begin();
for(int dwn = 0; dwn < work.getPixels( ); dwn++){
for(int acc = 0; acc < 2; acc++){
if(acc == (num) || acc == (num+1)){
temp = *itr;
if(temp >= 128){
bits[0] = 1;
temp=-128;
}
if(temp >= 64){
bits[1] =1;
temp-=64;
}
if(temp >= 32){
bits[2] =1;
temp-=32;
}
if(temp >= 16){
bits[3] =1;
temp-=16;
}
if(temp >= 8){
bits[4] =1;
temp-=8;
}
if(temp >= 4){
bits[5] =1;
temp-=4;
}
if(temp >= 2){
bits[6] =1;
temp-=2;
}
if(temp >= 1){
bits[7] =1;
}
table[0+(counter*8)] = bits[0];
table[1+(counter*8)] = bits[1];
table[2+(counter*8)] = bits[2];
table[3+(counter*8)] = bits[3];
table[4+(counter*8)] = bits[4];
table[5+(counter*8)] = bits[5];
table[6+(counter*8)] = bits[6];
table[7+(counter*8)] = bits[7];
for(int l = 0; l != 7; l++){bits[l]=0;}
counter++;
itr++;
}
}
}
}

It does not work. It messes up if the number is over 128 that is the
first bit 10000000 is above 128. Anything thing look wrong. I will
send my program or post more code if that is needed.

You can see a functioning version of my program at:
http://www.planetsourcecode.com/vb/s...11996&lngWId=3
Mar 31 '08 #1
11 1857
JoeC wrote:
I am working on a graphics program but my question has nothing to do
with graphics but trying to get an algorithm to work. I set graphics
from a 16x16 grid to bits of a graphic with:

bitData[index++] = binTemp[0] * 128 + binTemp[1] * 64 +
binTemp[2] * 32 + binTemp[3] * 16 +
binTemp[4] * 8 + binTemp[5] * 4 +
binTemp[6] * 2 + binTemp[7];

But I want to populate that same pad from bits:
void populate(int num){
int bits[8] = {0,0,0,0,0,0,0, 0};
int counter = 0;
int placer = 0;
BYTE temp;
clear();
std::vector<BYT Evb = work.getBits();
std::vector<BYT E>::const_itera tor itr = vb.begin();
for(int dwn = 0; dwn < work.getPixels( ); dwn++){
for(int acc = 0; acc < 2; acc++){
if(acc == (num) || acc == (num+1)){
temp = *itr;
if(temp >= 128){
bits[0] = 1;
temp=-128;
}
if(temp >= 64){
bits[1] =1;
temp-=64;
}
if(temp >= 32){
bits[2] =1;
temp-=32;
}
if(temp >= 16){
bits[3] =1;
temp-=16;
}
if(temp >= 8){
bits[4] =1;
temp-=8;
}
if(temp >= 4){
bits[5] =1;
temp-=4;
}
if(temp >= 2){
bits[6] =1;
temp-=2;
}
if(temp >= 1){
bits[7] =1;
}
table[0+(counter*8)] = bits[0];
table[1+(counter*8)] = bits[1];
table[2+(counter*8)] = bits[2];
table[3+(counter*8)] = bits[3];
table[4+(counter*8)] = bits[4];
table[5+(counter*8)] = bits[5];
table[6+(counter*8)] = bits[6];
table[7+(counter*8)] = bits[7];
for(int l = 0; l != 7; l++){bits[l]=0;}
counter++;
itr++;
}
}
}
}

It does not work. It messes up if the number is over 128 that is the
first bit 10000000 is above 128. Anything thing look wrong. I will
send my program or post more code if that is needed.

You can see a functioning version of my program at:
http://www.planetsourcecode.com/vb/s...11996&lngWId=3
You state it messes up if the number is over 128. Are you aware of the sign
bit? For a signed value, such as signed char, int, the high bit is used to
represent negative. If BYTE is char or signed char, then any value with the
high bit set will be negative.

I.E. 11111111 is -1. 11111110 is -2 using 2's compliment.

How is BYTE defined? Try making sure it's unsigned char and see if it then
works.

--
Jim Langston
ta*******@rocke tmail.com
Mar 31 '08 #2
In article <7b28c8e1-0dd7-48ee-ac33-40e7dc517139
@p25g2000hsf.go oglegroups.com> , en*****@yahoo.c om says...

[ ... ]
temp=-128;
My guess is that this isn't what you intended. I'd guess you meant '-='
instead of '=-'.

Interestingly enough, at one time in the history of C, this would
actually have done the same thing, but that's purely historical, and so
long ago I doubt it ever made it into C++ at all.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 31 '08 #3
Jerry Coffin wrote:
In article <7b28c8e1-0dd7-48ee-ac33-40e7dc517139
@p25g2000hsf.go oglegroups.com> , en*****@yahoo.c om says...

[ ... ]
> temp=-128;

My guess is that this isn't what you intended. I'd guess you meant '-='
instead of '=-'.

Interestingly enough, at one time in the history of C, this would
actually have done the same thing, but that's purely historical, and so
long ago I doubt it ever made it into C++ at all.
I think you're right.

But might I suggest using

bits[0] = (temp & 128) ? 1 : 0;

in place of

if(temp >= 128){
bits[0] = 1;
temp=-128;
}

It'll give the optimiser more chance to work.

Andy
Mar 31 '08 #4
Andy Champ wrote:
Jerry Coffin wrote:
>In article <7b28c8e1-0dd7-48ee-ac33-40e7dc517139
@p25g2000hsf.g ooglegroups.com >, en*****@yahoo.c om says...

[ ... ]
>> temp=-128;

My guess is that this isn't what you intended. I'd guess you meant
'-=' instead of '=-'.

Interestingl y enough, at one time in the history of C, this would
actually have done the same thing, but that's purely historical, and
so long ago I doubt it ever made it into C++ at all.

I think you're right.

But might I suggest using

bits[0] = (temp & 128) ? 1 : 0;

in place of

if(temp >= 128){
bits[0] = 1;
temp=-128;
}

It'll give the optimiser more chance to work.
Doesn't look equivalent. The value of 'temp' doesn't change in the
expression case, and it does in the 'if' case.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 31 '08 #5
Victor Bazarov wrote:
Andy Champ wrote:
>Jerry Coffin wrote:
>>In article <7b28c8e1-0dd7-48ee-ac33-40e7dc517139
@p25g2000hsf. googlegroups.co m>, en*****@yahoo.c om says...

[ ... ]

temp=-128;
My guess is that this isn't what you intended. I'd guess you meant
'-=' instead of '=-'.

Interesting ly enough, at one time in the history of C, this would
actually have done the same thing, but that's purely historical, and
so long ago I doubt it ever made it into C++ at all.
I think you're right.

But might I suggest using

bits[0] = (temp & 128) ? 1 : 0;

in place of

if(temp >= 128){
bits[0] = 1;
temp=-128;
}

It'll give the optimiser more chance to work.

Doesn't look equivalent. The value of 'temp' doesn't change in the
expression case, and it does in the 'if' case.
I was just about to post the same response when I realised the suggested
form does not require temp to be updated, all the bits can be tested in
place.

--
Ian Collins.
Mar 31 '08 #6
Ian Collins wrote:
Victor Bazarov wrote:
>Andy Champ wrote:
>>Jerry Coffin wrote:
In article <7b28c8e1-0dd7-48ee-ac33-40e7dc517139
@p25g2000hsf .googlegroups.c om>, en*****@yahoo.c om says...

[ ... ]

temp=-128;
My guess is that this isn't what you intended. I'd guess you meant
'-=' instead of '=-'.

Interestingl y enough, at one time in the history of C, this would
actually have done the same thing, but that's purely historical,
and so long ago I doubt it ever made it into C++ at all.

I think you're right.

But might I suggest using

bits[0] = (temp & 128) ? 1 : 0;

in place of

if(temp >= 128){
bits[0] = 1;
temp=-128;
}

It'll give the optimiser more chance to work.

Doesn't look equivalent. The value of 'temp' doesn't change in the
expression case, and it does in the 'if' case.
I was just about to post the same response when I realised the
suggested form does not require temp to be updated, all the bits can
be tested in place.
I agree; my reply was out of context.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 31 '08 #7
On Mar 31, 3:20 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Ian Collins wrote:
Victor Bazarov wrote:
Andy Champ wrote:
Jerry Coffin wrote:
In article <7b28c8e1-0dd7-48ee-ac33-40e7dc517139
@p25g2000hsf. googlegroups.co m>, enki...@yahoo.c om says...
>>[ ... ]
>>> temp=-128;
My guess is that this isn't what you intended. I'd guess you meant
'-=' instead of '=-'.
>>Interesting ly enough, at one time in the history of C, this would
actually have done the same thing, but that's purely historical,
and so long ago I doubt it ever made it into C++ at all.
>I think you're right.
>But might I suggest using
>bits[0] = (temp & 128) ? 1 : 0;
>in place of
>if(temp >= 128){
bits[0] = 1;
temp=-128;
}
>It'll give the optimiser more chance to work.
Doesn't look equivalent. The value of 'temp' doesn't change in the
expression case, and it does in the 'if' case.
I was just about to post the same response when I realised the
suggested form does not require temp to be updated, all the bits can
be tested in place.

I agree; my reply was out of context.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks all for the help:
if(temp >= 128){
bits[0] = 1;
temp-=128;
did come out better, it is always good to have anther set of eyes.
Still, I am not sure how I could do this different. The suggestions
are good.

It does work better it is just the first bit is always on or always
off depending on how I do the last statment:

if(temp >= 2){
bits[6] =1;
temp-=2;
}
if(temp >= 1){
bits[7] =1;
}

I do have some more issues with the program but I will try to resolve
them before I ask for help.
Mar 31 '08 #8
* JoeC:
It does not work. It messes up if the number is over 128 that is the
first bit 10000000 is above 128. Anything thing look wrong. I will
send my program or post more code if that is needed.
As others have already mentioned else-thread, signedness is important (cast to
unsigned to extract bits).

For the bit extraction I suggest using std::bitset.

It will probably not be more efficient than your current code, but easier to get
right, and much shorter code.
Cheers, & hth.,

- Alf
PS: for efficient bitmapped graphics operations you'll probably benefit from
looking at so called "blitter" or "bitblt" operations. Unfortunately not part
of current standard C++ library. Would have been nice to have there.
Mar 31 '08 #9
In article <7b28c8e1-0dd7-48ee-ac33-40e7dc517139
@p25g2000hsf.go oglegroups.com> , en*****@yahoo.c om says...

[ ... ]

A few more comments on your code:
if(temp >= 128){
bits[0] = 1;
temp=-128;
}
if(temp >= 64){
bits[1] =1;
temp-=64;
}
if(temp >= 32){
bits[2] =1;
temp-=32;
}
if(temp >= 16){
bits[3] =1;
temp-=16;
}
if(temp >= 8){
bits[4] =1;
temp-=8;
}
if(temp >= 4){
bits[5] =1;
temp-=4;
}
if(temp >= 2){
bits[6] =1;
temp-=2;
}
if(temp >= 1){
bits[7] =1;
}
I should have pointed out that I think there are better ways to do this.
One possibility using the ternary operator has already been pointed out.
Here's another possible alternative:

bits[0] = (temp >7) & 1;
bits[1] = (temp >6) & 1;
bits[2] = (temp >5) & 1;
bits[3] = (temp >4) & 1;
bits[4] = (temp >3) & 1;
bits[5] = (temp >2) & 1;
bits[6] = (temp >1) & 1;
bits[7] = (temp >0) & 1;

Of course, turning that into a loop is decidedly trivial.
table[0+(counter*8)] = bits[0];
table[1+(counter*8)] = bits[1];
table[2+(counter*8)] = bits[2];
table[3+(counter*8)] = bits[3];
table[4+(counter*8)] = bits[4];
table[5+(counter*8)] = bits[5];
table[6+(counter*8)] = bits[6];
table[7+(counter*8)] = bits[7];
You could also combine the two steps, moving data directly from temp to
table, without using bits[] at all.
for(int l = 0; l != 7; l++){bits[l]=0;}
I'd suggest that you never use 'l' as a variable name again. In many
fonts, it's quite easy to mistake for a '1', hurting readability a great
deal.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Apr 1 '08 #10

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

Similar topics

0
1153
by: It's me | last post by:
I've built a Python application using PythonCard 1.9 and Python 2.3 running under Windows XP. Everything works except that when I use the keyboard instead of the mouse to do certain operations in a data entry field (like Shift-Home), the program stops at line 1014 of wx-2.5.3-msw.ansi\wx\_core.py. def __getitem__(self, index): try: x = self.Get() except IndexError:
2
1843
by: isaac | last post by:
Sorry in advance if this is the incorrect board, but I couldn't find a better-targeted one. I want an IDE that supports building and debugging C++/qt on both Windows and Solaris. The Qt/Windows page seems to say that only Visual Studio/Borland ide's are supported--I assume that debugging can only be done in these ide's? Can I use 2 separate qt-supported IDEs on Solaris ( K-develop ) and Windows ( Visual Studio .NET ), but use qmake as...
1
1823
by: Kurt Krueckeberg | last post by:
In the book C++ Gothcas, Gotcha #7 is an example of using boolean logic to simply code. My question follows this snippet from the book. "Do you have to count to eight when presented with the following?" int ctr = 0; for (int i =0; i < 8; ++i) { if (options & 1 << (8+i) ) if ( ctr++) { cerr << "too many options selected"; break;
7
4941
by: Advocated | last post by:
Hi there, just wondering if anyone uses visual studio 6? Im having real problems, just with debugging, i could either do with a really good tutorial, if anyone knows of one, but the main thing is, when i run the debugger, as soon as it gets to my first printf statement, its asking me "please enter the path for PRINTF.C" Any ideas?
10
6857
by: Brian W | last post by:
Hi All, I have a web user control that, among other things, provides Print this page, and Email this page functionality I have this script that is to execute on the click of the asp:hyperlinks I have a function in a <SCRIPT> block that I want in the <head></head> section of the page. Unfortunately, RegisterClientScriptBlock, RegisterStartupScript don't always work, and when they do the script is placed inside the <form> tag (this...
38
2510
by: Till Crueger | last post by:
Hi, I stumbled upon the following code to determine byte ordering in the FAQ: union { int i; char c; } x; /* do stuff */
1
2355
by: OriginalCopy | last post by:
This is a demonstrative code which could be used for debugging purposes. And yet I don't know how to insert the necessary data on line 63, purely syntactically speaking ? I'm a beginner with STL, and from what I've read insert() accepts references for almost all of its overloaded versions. If so, what could one do so those element's memory space doesn't wipe out? I suppose one should use the heap. In rest I hope the code speaks for itself, it's...
18
2507
by: subramanian100in | last post by:
Consider a class that has vector< pair<int, string>* c; as member data object. I need to use operator>to store values into this container object and operator<< to print the contents of the container. I have written both these operators as non-friend functions.
42
4538
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector of string, in one line. I could
0
8999
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
9575
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9394
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...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
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
6080
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
4712
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
3322
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
3
2223
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.