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

Reading .bmp file in C

Hi all,
I want to read the pixel values of a .bmp image(which will be input to
the code) into a matrix...plz help me out as i can not understand how
to achieve this...

thanks in advance

biplab
Sep 10 '08 #1
41 5329
biplab said:
Hi all,
I want to read the pixel values of a .bmp image(which will be input to
the code) into a matrix...plz help me out as i can not understand how
to achieve this...
What have you tried so far?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 10 '08 #2
On Sep 10, 6:12*pm, Richard Heathfield <r...@see.sig.invalidwrote:
biplab said:
Hi all,
I want to read the pixel values of a .bmp image(which will be input to
the code) into a matrix...plz help me out as i can not understand how
to achieve this...

What have you tried so far?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
I have read the file using fopen...and then read the value...bt
nothing is read...I have a code in C which access the vedio memory to
print an image....can you please help me out from that code how can i
read the pixel vales and have the same image as output as the input...

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <mem.h>
#include <string.h>
#include <conio.h>
#include <fcntl.h>
#include <io.h>
#include <math.h>

unsigned char r[360],mini,mode;
unsigned char rgb[256][3];
char *fullname;
char *fname;
unsigned int screen_width=320;
unsigned int first_row=0,first_column=0,last_row=0,last_column= 0;
int handle,i,j,rem,color_bits;
long int
file_size,data_offset,size_info_header,width,heigh t,compression,
image_size,xpelspermeter,ypelspermeter,colorsused, colorsimpt;
float theta;
void drawdot(int y, int x, int pal_index){
_AX=pal_index;
_CX=x;
_DX=y;
_AH=0x0c;
_BH=0x00;
geninterrupt(0x10);
}

void main(){

gets(fname);
strcpy(fullname,fname);
strcat(fullname,".bmp");
handle=_open(fullname,O_RDONLY);
if (handle==-1) exit(EXIT_FAILURE);
_read(handle,r,2);
if (r[0] !='B' | r[1] !='M') exit(EXIT_FAILURE);
_read(handle,r,
4);file_size=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,2);
_read(handle,r,2);
_read(handle,r,4);
data_offset=r[0]+256*r[1]+65536*r[2]+0x1000000*r[3];
_read(handle,r,4);
size_info_header=r[0]+256*r[1]+65536*r[2]+0x1000000*r[3];
if (size_info_header !=40) exit(EXIT_FAILURE);
_read(handle,r,4);
width=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
if (width screen_width) exit(0);
_read(handle,r,4);
height=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,2);
if (r[0] != 1) exit(EXIT_FAILURE);
_read(handle,r,2);
color_bits=r[0];
_read(handle,r,4);
compression=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,4);
image_size=r[0]+256*r[1]+256*256*r[2]+256*256*256*r[3];
_read(handle,r,4);
xpelspermeter=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,4);
ypelspermeter=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,4);
colorsused=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,4);
colorsimpt=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_AX=0x1201;_BL=0x33;geninterrupt(0x10);//don't add RGB
_AX=0x0013;geninterrupt(0x10);//set mode 13 i.e. 320x200x256

for (i=0;i<256;i++) {
_read(handle,r,4);
rgb[i][0]=r[2];rgb[i][1]=r[1];rgb[i][2]=r[0];
}

_ES=FP_SEG(rgb);_DX=FP_OFF(rgb);
_BX=0;_CX=0x100; _AX=0x1012;
geninterrupt(0x10);//set DAC from rgb

first_column=(screen_width-width)/2;
last_row=first_row+height-1;
last_column=first_column+width-1;
rem=4 - (width % 4);
for (i=0;i<=height-1;i++) {
_read(handle,r,width);
for (j=0;j<=width-1;j++) drawdot(first_column+j,last_row-i,r[j]);
if (rem==1) _read(handle,r,1);
else if (rem==2) _read(handle,r,2);
else if (rem==3) _read(handle,r,3);//getch();
}
_close(handle);
getch();
_AX=0x1201;_BL=0x33;geninterrupt(0x10);//don't add RGB
_AX=0x0003;geninterrupt(0x10);//set mode 3 i.e. 16-color text

}

1st of all i will be highly obliged if you let me know what the code
is doing and how??/As i have found the code in the internet and am a
newbie in C...
thanks in advance...
biplab
Sep 10 '08 #3
biplab wrote:
On Sep 10, 6:12 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>biplab said:
I have read the file using fopen...and then read the value...bt
nothing is read...I have a code in C which access the vedio memory to
print an image....can you please help me out from that code how can i
read the pixel vales and have the same image as output as the input...
unsigned char r[360],mini,mode;
<snip bad code>
}

1st of all i will be highly obliged if you let me know what the code
is doing and how??/As i have found the code in the internet and am a
newbie in C...
thanks in advance...
biplab
It seems to read a bmp file, very badly (and assumes it is 8-bit colour
indexed), then displays it in a highly machine-specific manner, which
probably won't work on a modern machine.

I suggest you find some more up-to-date code on the internet. And separate
out the bmp loading from the display. You might try google on "bmp file
format".

--
Bartc

Sep 10 '08 #4
On Sep 10, 8:21*am, biplab <ge*******@gmail.comwrote:

<snip>
1st of all i will be highly obliged if you let me know what the code
is doing and how??/As i have found the code in the internet and am a
newbie in C...
thanks in advance...
The code doesn't do much. It invokes undefined behavior

- at line 1 in main() (calling gets() with an uninitialized pointer)
- at line 2 in main() (calling strcpy() with an uninitialized
pointer)
- at line 3 in main() (calling strcat() with an uninitialized
pointer)
- at line 4 in main() (calling _open() with an uninitialized
pointer)

....and presumably a few dozen other times. The first three are the
same kind of UB: writing to unexistent/unowned memory. This is likely
going to crash the program.

Sebastian

Sep 10 '08 #5

"biplab" <ge*******@gmail.comwrote in message news
>I have read the file using fopen...and then read the value...bt
nothing is read...I have a code in C which access the vedio memory to
print an image....can you please help me out from that code how can i
read the pixel vales and have the same image as output as the input...
Don't revinvent the wheel. There's a portable BMP loader on my website that
you are welcome to take. Essentially it does what you were doing, examining
the binary file byte by byte to decode it, but all the special cases are
coded in and tested.

You need to know whether your display system is a modern one that handles
truecolor images, or an old-fashioned one that uses color-index mode.
If the former, you wnat to call loadbmp() to retrieve rgb values. If the
latter, loadbmp8bit() to get the colour index values and the palette.

Then look in the Microsoft documentation to determine how to create a
skeleton windows application. You can then creagte a window of the desired
size and write the image to it, pixel by pixel. Once you get this working
you can try the faster block bit transfer, if performance is a problem.

The assembly interrupts probably won't work any more.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 10 '08 #6
On Sep 10, 9:47 pm, s0s...@gmail.com wrote:
On Sep 10, 8:21 am, biplab <getbip...@gmail.comwrote:

<snip>
1st of all i will be highly obliged if you let me know what the code
is doing and how??/As i have found the code in the internet and am a
newbie in C...
thanks in advance...

The code doesn't do much. It invokes undefined behavior

- at line 1 in main() (calling gets() with an uninitialized pointer)
Well, not really.
That code would fail to translate, because of the non-standard
headers. If not, how can you guess the contents of the non-standard
headers?
For example, <dos.hmight had:

#undef gets
#define gets(x) (void)0

<snip>

Sep 10 '08 #7

"Malcolm McLean" <re*******@btinternet.comwrote in message
news:vc******************************@bt.com...
Don't revinvent the wheel. There's a portable BMP loader on my website
that you are welcome to take. Essentially it does what you were doing,
examining the binary file byte by byte to decode it, but all the special
cases are coded in and tested.

You need to know whether your display system is a modern one that handles
truecolor images, or an old-fashioned one that uses color-index mode.
If the former, you wnat to call loadbmp() to retrieve rgb values. If the
latter, loadbmp8bit() to get the colour index values and the palette.
[snip]

Malcom can you mention these values. I might be interested in taking a swing
at this myself. You mention byte by byte. Since a char is a byte would you
recommend using getc and putc or fread ? I would be interested in the
symantics of your code.

Bill
Sep 10 '08 #8
On Wed, 10 Sep 2008 06:21:59 -0700 (PDT), biplab <ge*******@gmail.com>
wrote:
>I have read the file using fopen...and then read the value...bt
nothing is read...I have a code in C which access the vedio memory to
print an image....can you please help me out from that code how can i
read the pixel vales and have the same image as output as the input...
snip some really bad code
>1st of all i will be highly obliged if you let me know what the code
is doing and how??/As i have found the code in the internet and am a
newbie in C...
Make a note of whatever site you found that code on and NEVER visit it
again. The code is absolutely hopeless, much worse than merely
obsolete. You need to start again from scratch.

Use only standard headers and functions so the people here can
help you. If you must use system specific features, ask in a
newsgroup that discusses that system.

The use of that many global variables is a 99% indicator of a
sloppy design.

Look in your reference for the correct signature of main().

You are attempting to read binary data. Open the file for that
mode and use an input function that supports it, such as fread.

Make sure every pointer you use points somewhere before you use
it.

Use horizontal white space to make your code readable.

--
Remove del for email
Sep 11 '08 #9
On Wed, 10 Sep 2008 05:57:49 -0700 (PDT), biplab posted:
Hi all,
I want to read the pixel values of a .bmp image(which will be input to
the code) into a matrix...plz help me out as i can not understand how
to achieve this...

thanks in advance

biplab
Standard C is the wrong tool for this. Change tools.
--
War will never cease until babies begin to come into the world with larger
cerebrums and smaller adrenal glands. 2
H. L. Mencken
Sep 11 '08 #10
Ron Ford <r...@example.invalidwrote:
biplab posted:
I want to read the pixel values of a .bmp image(which
will be input to the code) into a matrix...plz help me
out as i can not understand how to achieve this...

Standard C is the wrong tool for this.
Why do you say that?

--
Peter
Sep 11 '08 #11
On Wed, 10 Sep 2008 19:44:06 -0700 (PDT), Peter Nilsson posted:
Ron Ford <r...@example.invalidwrote:
>biplab posted:
>>I want to read the pixel values of a .bmp image(which
will be input to the code) into a matrix...plz help me
out as i can not understand how to achieve this...

Standard C is the wrong tool for this.

Why do you say that?
Because extensions make this easy.
--
War will never cease until babies begin to come into the world with larger
cerebrums and smaller adrenal glands. 2
H. L. Mencken
Sep 11 '08 #12

"Ron Ford" <ro*@example.invalidwrote in message
>
Because extensions make this easy.
You're right. For instance BMP files can be jpeg-compressed. This happens
but rarely, but it is in the file format. You are much better off calling an
image-loading routine supplied by Microsoft.
Unless that is you are not on a Windows machine.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 11 '08 #13

"Malcolm McLean" <re*******@btinternet.comwrote in message
news:5q******************************@bt.com...
However if we say

int width;

width = fgetc(fp);
width |= fgetc(fp) << 8;

Then we retrieve the right value of width, regardless of whether integers
are 2 bytes or 4 bytes, or big- or little- endian.
Ok I have no real experience with the binary operators so I'm going to
say your pipe symbol | is XOR. << is the left shift operator for an 8 bit
value. I really don't know yet how to use this in real time but I recognize
the symbols.

Bill
Sep 11 '08 #14
On Sep 11, 11:46 pm, "Bill Cunningham" <nos...@nspam.comwrote:
"Malcolm McLean" <regniz...@btinternet.comwrote in message

news:5q******************************@bt.com...
However if we say
int width;
width = fgetc(fp);
width |= fgetc(fp) << 8;
Then we retrieve the right value of width, regardless of whether integers
are 2 bytes or 4 bytes, or big- or little- endian.

Ok I have no real experience with the binary operators so I'm going to
say your pipe symbol | is XOR.
lol.
Sep 11 '08 #15
Bill Cunningham said:

<snip>
Ok I have no real experience with the binary operators so I'm going
to
say your pipe symbol | is XOR.
That's the wrong way to learn. Instead of guessing, consult your C
reference, which makes the roles of bitwise operators perfectly clear.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 11 '08 #16
vi******@gmail.com writes:
On Sep 10, 9:47 pm, s0s...@gmail.com wrote:
>On Sep 10, 8:21 am, biplab <getbip...@gmail.comwrote:

<snip>
1st of all i will be highly obliged if you let me know what the code
is doing and how??/As i have found the code in the internet and am a
newbie in C...
thanks in advance...

The code doesn't do much. It invokes undefined behavior

- at line 1 in main() (calling gets() with an uninitialized pointer)

Well, not really.
Well, yes, really.
That code would fail to translate, because of the non-standard
headers.
It *might* fail to translate, depending on the implementation. Unless
there's a statement in the standard that I've missed that says
anything that includes a non-standard header must fail to translate.
(Hint: There isn't.)
If not, how can you guess the contents of the non-standard
headers?
For example, <dos.hmight had:

#undef gets
#define gets(x) (void)0
You can't *know* the contents of non-standard headers, but you can
certainly *guess*. For example, I guess, with a fairly high level of
confidence, that a <dos.hheader in any implementation that provides
it *doesn't* have the above macro definition.

The fact that <dos.his non-standard, and therefore off-topic in this
newsgroup, doesn't mean that we can or should pretend that it doesn't
exist or that it's likely to contain some implausible content.
Writing non-portable code is one of the main purposes of C, after all.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 11 '08 #17
"Bill Cunningham" <no****@nspam.comwrites:
"Malcolm McLean" <re*******@btinternet.comwrote in message
news:5q******************************@bt.com...
>However if we say

int width;

width = fgetc(fp);
width |= fgetc(fp) << 8;

Then we retrieve the right value of width, regardless of whether integers
are 2 bytes or 4 bytes, or big- or little- endian.

Ok I have no real experience with the binary operators so I'm going to
say your pipe symbol | is XOR. << is the left shift operator for an 8 bit
value. I really don't know yet how to use this in real time but I recognize
the symbols.
They're better referred to as bitwise operators. The term "binary
operator" commonly refers to an operator that takes two operands.

Your lack of experience with bitwise operators is no excuse for
*guessing* what they mean. "|" is not XOR. Consult your copy of
K&R2 (you've said you have one) to learn what it is, and what the
actual XOR operator looks like.

(Yes, << is the left shift operator, but it's not specific to 8
bit values.)

Last month, I gave you some advice about
the use of standard library functions. See
<http://groups.google.com/group/comp.lang.c/msg/6da991ad26d30ed9>.
Everything I wrote there applies equally to predefined operators,
at least to any that you're not familiar with. (At least this
time you're not writing code based on your guesses.)

One more thing: you're using <no****@nspam.comas your return
address, obviously to avoid spam. nspam.com happens to be a
real domain. I'm sure its owner doesn't appreciate getting
all the spam that would have gone to you. Use something like
<no****@nospam.invalid(see RFC 2606).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 11 '08 #18
On Sep 12, 12:29 am, Keith Thompson <ks...@mib.orgwrote:
vipps...@gmail.com writes:
On Sep 10, 9:47 pm, s0s...@gmail.com wrote:
The code doesn't do much. It invokes undefined behavior
- at line 1 in main() (calling gets() with an uninitialized pointer)
Well, not really.

Well, yes, really.
That code would fail to translate, because of the non-standard
headers.

It *might* fail to translate, depending on the implementation. Unless
Reading further,
"if not [...]"
So I did not say that it _must_ fail. I examined each posibility.
(I accept that this may not have been obvious, but these were my
intentions)
there's a statement in the standard that I've missed that says
anything that includes a non-standard header must fail to translate.
(Hint: There isn't.)
If not, how can you guess the contents of the non-standard
headers?
For example, <dos.hmight had:
#undef gets
#define gets(x) (void)0

You can't *know* the contents of non-standard headers, but you can
certainly *guess*. For example, I guess, with a fairly high level of
confidence, that a <dos.hheader in any implementation that provides
it *doesn't* have the above macro definition.

The fact that <dos.his non-standard, and therefore off-topic in this
newsgroup, doesn't mean that we can or should pretend that it doesn't
exist or that it's likely to contain some implausible content.
Writing non-portable code is one of the main purposes of C, after all.
I disagree. After all, nasal demons were given life here. :-)
(according to the jargon file, comp.std.c actually)
Being more serious here, of course that gets call invoked undefined
behavior. However, don't you agree my post had some truth hidden in
it? You can't assume what happends if you don't know what the headers
do.

Plus, who knows, there might be an implementation whose header (one of
the non-standard headers) does have such (or similar) macro
definition, to prevent people from using gets.
Sep 11 '08 #19
"Bill Cunningham" <no****@nspam.comwrites:
"Malcolm McLean" <re*******@btinternet.comwrote in message
news:5q******************************@bt.com...
>However if we say

int width;

width = fgetc(fp);
width |= fgetc(fp) << 8;

Then we retrieve the right value of width, regardless of whether integers
are 2 bytes or 4 bytes, or big- or little- endian.

Ok I have no real experience with the binary operators so I'm going to
say your pipe symbol | is XOR. << is the left shift operator for an 8 bit
value. I really don't know yet how to use this in real time but I recognize
the symbols.

Bill
Wonderful Bill! You're trolling knows no bounds!

Pipe symbol is XOR. Priceless.
Sep 11 '08 #20
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
....
>Writing non-portable code is one of the main purposes of C, after all.
Whoa!! What happened here??? Who hacked into Keith's account?

Let's be honest about this: How can you say "One of the main purposes
of X is to do Y, but you can't discuss Y in the newsgroup dedicated to X?"
It is simply absurd.

Sep 12 '08 #21

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
One more thing: you're using <no****@nspam.comas your return
address, obviously to avoid spam. nspam.com happens to be a
real domain. I'm sure its owner doesn't appreciate getting
all the spam that would have gone to you. Use something like
<no****@nospam.invalid(see RFC 2606).
Ok thanks for letting me know.

Bill
Sep 12 '08 #22

"Richard" <rg****@gmail.comwrote in message
news:ga**********@registered.motzarella.org...
Wonderful Bill! You're trolling knows no bounds!

Pipe symbol is XOR. Priceless.
It's a bitwise or, as opposed to ||. Not very funny. I've never used the
bitwise operators so I'm going to read up on them.

Bill
Sep 12 '08 #23
I'm getting better Keith with the functions. But things like the bitwise
operators as a perfect example I have never used. Pointers are another thing
not completely understood. When to declare a pointer and use & for example.
In reading a prototype I would have to experiment.

Bill
Sep 12 '08 #24
"Bill Cunningham" <no****@nspam.comwrites:
"Richard" <rg****@gmail.comwrote in message
news:ga**********@registered.motzarella.org...
>Wonderful Bill! You're trolling knows no bounds!

Pipe symbol is XOR. Priceless.

It's a bitwise or, as opposed to ||. Not very funny. I've never used the
bitwise operators so I'm going to read up on them.
As opposed to what? Yo think || is XOR now?

Watch out Linux Torwalds! There's a new man in town. Seriously. Give it
up. You're too transparent. No one can be this clueless.
>
Bill

--
Sep 12 '08 #25
Ok I guess now after looking at wikipedia that XOR is ^ and | is OR. I
have not really encountered a use yet for these operators.

Bill
Sep 12 '08 #26
"Bill Cunningham" <no****@nspam.invalidwrites:
Ok I guess now after looking at wikipedia that XOR is ^ and | is OR. I
have not really encountered a use yet for these operators.

Bill
You don't have a copy of K&R?
Sep 12 '08 #27
On Thu, 11 Sep 2008 21:29:53 -0400, "Bill Cunningham"
<no****@nspam.invalidwrote:
I'm getting better Keith with the functions. But things like the bitwise
operators as a perfect example I have never used. Pointers are another thing
not completely understood. When to declare a pointer and use & for example.
In reading a prototype I would have to experiment.
It doesn't matter whether you have ever used it or not. It is the
idiocy of guessing instead of researching that people are commenting
on.

--
Remove del for email
Sep 12 '08 #28
"Bill Cunningham" <no****@nspam.invalidwrites:
Ok I guess now after looking at wikipedia that XOR is ^ and | is OR. I
have not really encountered a use yet for these operators.
You've said you have a copy of K&R2. Why the ^|&~ would you consult
Wikipedia?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 12 '08 #29
Bill Cunningham said:
Ok I guess now after looking at wikipedia that XOR is ^ and | is OR.
Stop guessing. Start reading K&R2.
I have not really encountered a use yet for these operators.
Then ignore them, or learn about them, at your leisure. But guessing is
just ludicrous.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 12 '08 #30
Bill Cunningham wrote:
Ok I guess now after looking at wikipedia that XOR is ^ and | is OR. I
have not really encountered a use yet for these operators.
I routinely avoid reading discussions that you start; I'm not certain
that you're the troll that some people think you are, but I find it
psychically painful to see lots of good advice aimed at your head which
just bounces off without ever penetrating. Over more than a decade a lot
of good-hearted and very competent people have put a lot of effort into
trying to teach you C. Those same people could have spent the same
amount of effort to turn dozens of ordinary students into expert C
programmers; but it seems to have been utterly wasted on you.

However, I don't have any way to tell my web browser to filter out
entire threads on the basis of the fact that you started the thread; so
occasionally I do end up reading someone else's responses to you.

You've been repeatedly told not to guess at the answers, but to figure
them out. You apologize each time, but continue doing it. You've been
repeatedly told that your copy of K&R is a much better place to look for
answers than virtually any other source you are in the habit of using,
but it seems as though you almost never use K&R. You apologize each
time, and keep doing it.

There's unfortunately nothing unusual about people failing to take good
advice; but I'm curious if, instead of an apology, you could give us any
actual reason you had for ignoring the advice, even if it is a bad reason.
Sep 12 '08 #31
James Kuyper <ja*********@verizon.netwrites:
Bill Cunningham wrote:
> Ok I guess now after looking at wikipedia that XOR is ^ and | is
OR. I have not really encountered a use yet for these operators.

I routinely avoid reading discussions that you start; I'm not certain
that you're the troll that some people think you are, but I find it
psychically painful to see lots of good advice aimed at your head
which just bounces off without ever penetrating. Over more than a
decade a lot of good-hearted and very competent people have put a lot
of effort into trying to teach you C. Those same people could have
spent the same amount of effort to turn dozens of ordinary students
into expert C programmers; but it seems to have been utterly wasted on
you.

However, I don't have any way to tell my web browser to filter out
entire threads on the basis of the fact that you started the thread;
so occasionally I do end up reading someone else's responses to you.

You've been repeatedly told not to guess at the answers, but to figure
them out. You apologize each time, but continue doing it. You've been
repeatedly told that your copy of K&R is a much better place to look
for answers than virtually any other source you are in the habit of
using, but it seems as though you almost never use K&R. You apologize
each time, and keep doing it.

There's unfortunately nothing unusual about people failing to take
good advice; but I'm curious if, instead of an apology, you could give
us any actual reason you had for ignoring the advice, even if it is a
bad reason.
I suspect he's the alter-ego of one of the ruder "regs". They feel they
have amended for their hateful, egotistical ways by responding politely
and calmly to someone who is obviously either simple or a troll. I think
troll but he does it so well... As Robert Downey Jr says (in the role of
a black soldier following a skin pigment augmentation) - "You made the
mistake of going 'full retard'".
Sep 12 '08 #32

"Bill Cunningham" <no****@nspam.comwrote in message
It's a bitwise or, as opposed to ||. Not very funny. I've never used
the bitwise operators so I'm going to read up on them.
At first sight the bitwise operators may appear entirely useless. Who wants
to set a bit to one if an only if two bits in the inputs are set?

However you'll soon realise that they are in fact very useful.

For instance

if( x & 1) /* if x is an odd number */

x & = 0xFF00; /* clear the lower 8 bits of x, also any bits above 16 */

lightswitch ^= 1; /* a toggle. The lightswitch goes on if it is off and vice
versa */

x <<= 1; /* these two lines will collect a series of bits in
integer */
x |= newbit; /* x, for instance for a compression algorithm */
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Sep 12 '08 #33

"Malcolm McLean" <re*******@btinternet.comwrote in message
news:bJ******************************@bt.com...
At first sight the bitwise operators may appear entirely useless. Who
wants to set a bit to one if an only if two bits in the inputs are set?

However you'll soon realise that they are in fact very useful.

For instance

if( x & 1) /* if x is an odd number */

x & = 0xFF00; /* clear the lower 8 bits of x, also any bits above 16 */

lightswitch ^= 1; /* a toggle. The lightswitch goes on if it is off and
vice versa */

x <<= 1; /* these two lines will collect a series of bits in
integer */
x |= newbit; /* x, for instance for a compression algorithm */
Is the x you are working with a char ?

Bill
Sep 12 '08 #34

"Bill Cunningham" <no****@nspam.invwrote in message
Malcolm
>For instance

if( x & 1) /* if x is an odd number */

x & = 0xFF00; /* clear the lower 8 bits of x, also any bits above 16 */

lightswitch ^= 1; /* a toggle. The lightswitch goes on if it is off and
vice versa */

x <<= 1; /* these two lines will collect a series of bits in
integer */
x |= newbit; /* x, for instance for a compression algorithm */

Is the x you are working with a char ?
Could be an unsigned char. Or could be an int. Except for the second
example, where we have at least 16 bits, and chars are almost always 8 bits.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 12 '08 #35
"Bill Cunningham" <no****@nspam.invwrites:
"Malcolm McLean" <re*******@btinternet.comwrote in message
news:bJ******************************@bt.com...
>At first sight the bitwise operators may appear entirely useless. Who
wants to set a bit to one if an only if two bits in the inputs are set?

However you'll soon realise that they are in fact very useful.

For instance

if( x & 1) /* if x is an odd number */

x & = 0xFF00; /* clear the lower 8 bits of x, also any bits above 16 */

lightswitch ^= 1; /* a toggle. The lightswitch goes on if it is off and
vice versa */

x <<= 1; /* these two lines will collect a series of bits in
integer */
x |= newbit; /* x, for instance for a compression algorithm */

Is the x you are working with a char ?
Will either a yes or no answer to that question be of any use to you?

The bitwise operators work on integer (and yes, char is an integer
type). Most of the time they *should* be used on unsigned integers
(char may be either signed or unsigned).

I don't think you have enough understanding of the bitwise operators
for your question to be meaningful. K&R2 discusses these operators.
Come back when you've read it.

BTW, I see you're now using <no****@nspam.invas your return address.
My advice was to use ".invalid", not ".inv". The relevant RFC
guarantees that there will never be a ".invalid" top-level domain; it
makes no such guarantee about ".inv". This is yet another case where
you change something without understanding what you're doing.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 12 '08 #36

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
"Bill Cunningham" <no****@nspam.invalidwrites:
> Ok I guess now after looking at wikipedia that XOR is ^ and | is OR.
I
have not really encountered a use yet for these operators.

You've said you have a copy of K&R2. Why the ^|&~ would you consult
Wikipedia?
I happened to be online, looked up wikipedia quickly and found my
answer. Otherwise I would have to leave the computer immediately dig through
some books and thought I'd leave that for later.

Kand R2 is very much a mouthful, or should I say head ful. I have
learned more at times from other texts and the help I get here and other
places. Nevertheless I do consult it from time to time.

Bill
Sep 12 '08 #37

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:NJ******************************@bt.com...
Then ignore them, or learn about them, at your leisure. But guessing is
just ludicrous.
When I am ready to research them I will read kandr2 or something similar
if kandr2 is too much of a mouthful, and ask if confused accordingly. but I
immediately went to wikipedia to find answers fast while online.

Bill
Sep 12 '08 #38

"Barry Schwarz" <sc******@dqel.comwrote in message
news:3u********************************@4ax.com...
It doesn't matter whether you have ever used it or not. It is the
idiocy of guessing instead of researching that people are commenting
on.
What makes you think I am not researching things. I immediately went to
wikipedia to find out answers fast. I will read up on kandr2 when I decide
to use bitwise operators then if I am confused I will inquire here.

Bill
Sep 12 '08 #39
On Fri, 12 Sep 2008 17:33:04 -0400, "Bill Cunningham"
<no****@nspam.invalidwrote:
>
"Barry Schwarz" <sc******@dqel.comwrote in message
news:3u********************************@4ax.com.. .
>It doesn't matter whether you have ever used it or not. It is the
idiocy of guessing instead of researching that people are commenting
on.

What makes you think I am not researching things. I immediately went to
No you didn't. Your immediate response was to guess.
>wikipedia to find out answers fast. I will read up on kandr2 when I decide
You have been told repeatedly that Wikipedia is not authoritative. Why
use it when you have K&R?
>to use bitwise operators then if I am confused I will inquire here.
If you have no intention of using them in the near future, why did you
start this discussion? By your own admission, you won't remember it
when the time comes.

--
Remove del for email
Sep 13 '08 #40
In article <jGsyk.1084$jE1.658@trnddc03>, James Kuyper
<ja*********@verizon.netwrote:
Bill Cunningham wrote:
Ok I guess now after looking at wikipedia that XOR is ^ and | is OR. I
have not really encountered a use yet for these operators.

I routinely avoid reading discussions that you start; I'm not certain
that you're the troll that some people think you are, but I find it
psychically painful to see lots of good advice aimed at your head which
just bounces off without ever penetrating. Over more than a decade a lot
of good-hearted and very competent people have put a lot of effort into
trying to teach you C. Those same people could have spent the same
amount of effort to turn dozens of ordinary students into expert C
programmers; but it seems to have been utterly wasted on you.
I don't understand why anyone who's been here more than a week even reads
his posts. Whatever the reason, it's not worth time even reading, much
less answering, posts with his name on it. It's like optimizing a program:
don't waste time on the areas where your work won't make any difference.
Nothing personal, just that measurement has shown that effort spent
answering his posts is wasteful.
Sep 14 '08 #41

"blargg" <bl********@gishpuppy.comwrote in message
news:bl*************************@192.168.1.4...
In article <jGsyk.1084$jE1.658@trnddc03>, James Kuyper
<ja*********@verizon.netwrote:
>Bill Cunningham wrote:
Ok I guess now after looking at wikipedia that XOR is ^ and | is
OR. I
have not really encountered a use yet for these operators.

I routinely avoid reading discussions that you start; I'm not certain
that you're the troll that some people think you are, but I find it
I don't understand why anyone who's been here more than a week even reads
his posts. Whatever the reason, it's not worth time even reading, much
less answering, posts with his name on it. It's like optimizing a program:
don't waste time on the areas where your work won't make any difference.
Nothing personal, just that measurement has shown that effort spent
answering his posts is wasteful.
Sometimes Bill's posts start a discussion on some aspect or other that may
be of interest to others reading the newsgroup.

Although the meaning of ^ and | probably isn't one of those..

--
bartc

Sep 14 '08 #42

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

Similar topics

14
by: deko | last post by:
Do I need to use flock() when reading a file into an array? It's possible that the file in question could be open at the time the file('filename') request is made. I realize flock() is required...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
11
by: Matt DeFoor | last post by:
I have some log files that I'm working with that look like this: 1000000000 3456 1234 1000000001 3456 1235 1000020002 3456 1223 1000203044 3456 986 etc. I'm trying to read the file...
2
by: archana | last post by:
Hi all, I am new to asp.net (learning phase). I have to develop application in asp.net to read file from client pc and display statistics of that file to client. So my question is that to...
12
by: SAL | last post by:
Hello, Is it possible to read a CSV from the Client, and bind my Datagrid to the data in the CSV file without uploading the file to the Server first? I have tried and in Debug mode on my...
3
by: miss time | last post by:
Hi all, my java friends ^-^ I have next week quiz in reading file text ,and understand the topic very well. can any one give some question related to this topic .this help me more to...
6
by: Studlyami | last post by:
Is it possible to open a file for reading, while another process has it open for writing to it? One of the problems is that the process that is writing to it is written in C and is running on a...
1
Coldfire
by: Coldfire | last post by:
Hi, The strange problem i am having is, the input element of type='file' not reading file names after 20 file elements. It simple returns null on reading the 'name' of file. The code is...
1
by: bjoarn | last post by:
I have an Application C# handling file reading, building index on this file, using dll wrapped with SWIG. The dll is originaly programmed in C++. Dll reports back to the the C# programm throug...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.