473,385 Members | 1,780 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.

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<BYTEvb = work.getBits();
std::vector<BYTE>::const_iterator 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 1835
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<BYTEvb = work.getBits();
std::vector<BYTE>::const_iterator 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*******@rocketmail.com
Mar 31 '08 #2
In article <7b28c8e1-0dd7-48ee-ac33-40e7dc517139
@p25g2000hsf.googlegroups.com>, en*****@yahoo.com 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.googlegroups.com>, en*****@yahoo.com 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.googlegroups.com>, en*****@yahoo.com 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.
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.com>, en*****@yahoo.com 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.

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.com>, en*****@yahoo.com 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.

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...@comAcast.netwrote:
Ian Collins wrote:
Victor Bazarov wrote:
Andy Champ wrote:
Jerry Coffin wrote:
In article <7b28c8e1-0dd7-48ee-ac33-40e7dc517139
@p25g2000hsf.googlegroups.com>, enki...@yahoo.com 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.
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.googlegroups.com>, en*****@yahoo.com 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
In article <MP************************@news.sunsite.dk>,
jc*****@taeus.com says...

[ ... ]
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;
I should have added that if temp is negative, this could (at least
theoretically) give incorrect results. In reality, it normally won't,
but if there's any chance of its being negative, converting to unsigned
(or starting with an unsigned type) wouldn't hurt.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Apr 1 '08 #11
On Mar 31, 11:17 pm, Jerry Coffin <jcof...@taeus.comwrote:
In article <7b28c8e1-0dd7-48ee-ac33-40e7dc517139
@p25g2000hsf.googlegroups.com>, enki...@yahoo.com 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.
I did the best I could creating this program. I have not worked with
this syntax and I am not sure how it works. >is that a bit shift, I
have see them before but never really used them. I don't get the
reference & to l?. What I am also trying to do is have mutiple
graphics sheets so the data might look like ff 00 ff 00 ff 00 ff

FF00
FF00
FF00
Is how it will be displayed basically I use flat arrays and let the
accessor functions sort out the rest.

So my problem is the +/- bit. I will see if I can make it all
unsigned.
>
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.
OK, I generally use lp. This was so short, I didn't think it would
matter.
Apr 2 '08 #12

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

Similar topics

0
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...
2
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...
1
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...
7
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...
10
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 ...
38
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
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...
18
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...
42
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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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.