Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Tell me more about 64 bit integers

Reply
Thread Tools

Tell me more about 64 bit integers

 
 
Kenneth Lantrip
Guest
Posts: n/a
 
      11-07-2006
Im trying to learn a little more about C compilers.

Im trying to work with 64 bit numbers (integers) with the gcc compiler
in linux (ubuntu). I need to know how to make use of 64 bit numbers.

My test program as follows:

-----------------------------------------------------------------
#include <ncurses.h>
#define sleep(x) usleep(x * 1000) // adjust sleep functions to
milliseconds
typedef unsigned char byte; // values are 0-255
#define wlen 43 // how many bits long we are
interested in

int Int2Bin(int x, byte *y) {
int i, j;

y += wlen; *y-- = 0; j = 0;
for (i = 0; i < wlen; i++) {
*y-- = 48 + (x & 1);
if (x & 1) j = i + 1;
x >>= 1;
}
return j;
}

int CountOnes(byte *x) {
int i;

i = 0;
do {
i += 1 & (*x++ == 49);
} while (*x != 0);
return i;
}

int main(void) {

static long long Count = 1, t = 0x1FFFFF;
static byte bits[64];

initscr();
noecho();

do {
Int2Bin(t++, bits);
if (CountOnes(bits) == 21) {
printw("%s :: Count = %d\n", bits, Count++);
refresh();
}
} while (t < 0x7FFFFC00000); // error here over long

getch();
endwin();

printf("\nTest program completed successfully.\n\n");
return 0;
}

--------------------------------------------------------

Any help greatly appriciated.
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      11-08-2006
On Tue, 07 Nov 2006 14:39:43 -0600, Kenneth Lantrip
<> wrote in comp.lang.c:

> Im trying to learn a little more about C compilers.
>
> Im trying to work with 64 bit numbers (integers) with the gcc compiler
> in linux (ubuntu). I need to know how to make use of 64 bit numbers.


Make up your mind, are you trying to learn about 64 bit integers, or
about C compilers?

> My test program as follows:
>
> -----------------------------------------------------------------
> #include <ncurses.h>
> #define sleep(x) usleep(x * 1000) // adjust sleep functions to
> milliseconds
> typedef unsigned char byte; // values are 0-255
> #define wlen 43 // how many bits long we are
> interested in
>
> int Int2Bin(int x, byte *y) {
> int i, j;
>
> y += wlen; *y-- = 0; j = 0;
> for (i = 0; i < wlen; i++) {
> *y-- = 48 + (x & 1);


Don't do what you did in the line above. It is both absolutely
horrible and absolutely unnecessary. You have placed a "magic number"
directly in the code. Someone reading this code some day might spend
hours scratching his/her head trying to figure out what the 48 is for.

The number of hours you worked last week? The number of cups of
coffee you drank before lunch? The number of lines on a page? The
number of gray hairs you developed working on this code?

Now I happen to have looked into my crystal ball and divined the fact
that you are using 48 as the numeric code for the ASCII '0' character.
Which means that your code is guaranteed to fail if it is ever ported
to a platform that uses a different character set.

And it's totally unnecessary. All you need to do here is replace 48
with '0'. Then it will work on any platform what a conforming C
compiler now and forever, no matter what character set it uses. And
of course, it tells a reader of your program exactly what is going on,
so he/she won't have to wonder if 48 is the number of pimples on your
butt.

> if (x & 1) j = i + 1;
> x >>= 1;
> }
> return j;
> }
>
> int CountOnes(byte *x) {
> int i;
>
> i = 0;
> do {
> i += 1 & (*x++ == 49);


Oh, there you are, doing it again. You need to nip down to the local
pharmacy for some ointment, it's spreading, you've added another
pimple on the butt.

'1', not 49!

> } while (*x != 0);
> return i;
> }
>
> int main(void) {
>
> static long long Count = 1, t = 0x1FFFFF;
> static byte bits[64];
>
> initscr();
> noecho();
>
> do {
> Int2Bin(t++, bits);
> if (CountOnes(bits) == 21) {
> printw("%s :: Count = %d\n", bits, Count++);
> refresh();
> }
> } while (t < 0x7FFFFC00000); // error here over long
>
> getch();
> endwin();
>
> printf("\nTest program completed successfully.\n\n");
> return 0;
> }
>
> --------------------------------------------------------
>
> Any help greatly appriciated.


I can't compile your code, because it uses some non-standard headers
and function calls, so don't keep me in suspense. What exactly is the
error message? When asking about such a message, copy the text and
paste it into your post.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      11-08-2006

Kenneth Lantrip wrote:
> Im trying to learn a little more about C compilers.
>
> Im trying to work with 64 bit numbers (integers) with the gcc compiler
> in linux (ubuntu). I need to know how to make use of 64 bit numbers.
>
> My test program as follows:
>
> -----------------------------------------------------------------
> #include <ncurses.h>
> #define sleep(x) usleep(x * 1000) // adjust sleep functions to
> milliseconds
> typedef unsigned char byte; // values are 0-255
> #define wlen 43 // how many bits long we are
> interested in


Please don't use "//" comments - as you can see the news reader has
messed them up, and I'd have to do a lot of repair to be able to cut,
paste and compile your code.

Why use the non-standard curses code (which is Un*x-specified, unless
I'm much mistaken), in a posting to the C group? If you use stdio, we
can (try to) build and test your code on any platform.

....
> for (i = 0; i < wlen; i++) {
> *y-- = 48 + (x & 1);


As the man said, 48 (and 49) are magic numbers - you should be more
expressive. Equally, why was 43 used for wlen?

....
> } while (t < 0x7FFFFC00000); // error here over long


What error?
Could it have been this one ? "nc.c:44: warning: integer constant is
too large for "long" type"

If so, perhaps you need to make the constant a "long long" constant, by
adding "LL"...

 
Reply With Quote
 
xscottg@gmail.com
Guest
Posts: n/a
 
      11-09-2006
Kenneth Lantrip wrote:

>
> Im trying to work with 64 bit numbers (integers) with the gcc compiler
> in linux (ubuntu). I need to know how to make use of 64 bit numbers.
>


> [...]
> } while (t < 0x7FFFFC00000); // error here over long
> [...]


Boy, tough crowd. I haven't read this group in a while, and the first
two responses I see are more flame than useful. I don't think I'm
going to stick around here.

Anyway, the problem is that you need to add a suffix to the number to
let it know it isn't a plain (32 bit in this case) integer.
0x7FFFFC00000LL should work with GCC on most common platforms... This
declares a "long long" integer constant. I forget what the suffix
would be under Win32, but since you're using ncurses, I'm guessing
you're working under Linux/Unix.

Cheers.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      11-09-2006
wrote:
> Kenneth Lantrip wrote:
>
>> Im trying to work with 64 bit numbers (integers) with the gcc
>> compiler in linux (ubuntu). I need to know how to make use of
>> 64 bit numbers.

>
>> [...]
>> } while (t < 0x7FFFFC00000); // error here over long
>> [...]

>
> Boy, tough crowd. I haven't read this group in a while, and the
> first two responses I see are more flame than useful. I don't
> think I'm going to stick around here.


Not very amenable to constructive criticism, are you?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


 
Reply With Quote
 
Kenneth Lantrip
Guest
Posts: n/a
 
      11-09-2006
wrote:
> Kenneth Lantrip wrote:
>
>
>>Im trying to work with 64 bit numbers (integers) with the gcc compiler
>>in linux (ubuntu). I need to know how to make use of 64 bit numbers.
>>

>
>
>> [...]
>> } while (t < 0x7FFFFC00000); // error here over long
>> [...]

>
>
> Boy, tough crowd. I haven't read this group in a while, and the first
> two responses I see are more flame than useful. I don't think I'm
> going to stick around here.
>
> Anyway, the problem is that you need to add a suffix to the number to
> let it know it isn't a plain (32 bit in this case) integer.
> 0x7FFFFC00000LL should work with GCC on most common platforms... This
> declares a "long long" integer constant. I forget what the suffix
> would be under Win32, but since you're using ncurses, I'm guessing
> you're working under Linux/Unix.
>
> Cheers.


Thanks a ton... That is what I needed to know.

To answer some of the other ppls question without posting again... I was
wanting to know how to handle 64 bit numbers as constants (compiler
operation). I realise there are (were) lots of errors in the code I
previously listed.

Here is the completed test run code. I found that it compiles and runs
very nicely on a 64 computer with 64 bit Ubuntu (Linux). This is just
for those curious about what was needed

#include <stdio.h>
typedef unsigned char byte; // values are 0-255
#define wlen 43 // how many bits long we are interested in

int Int2Bin(unsigned long long x, byte *y) {
int i, j;

y += wlen; *y-- = 0; j = 0;
for (i = 0; i < wlen; i++) {
if (x & 1) {*y-- = 49; j++;} else *y-- = 48;
x >>= 1;
}
return j;
}

int main(void) {

static unsigned long long Count = 0;
static unsigned long long t = 0x3FFFFF;
static unsigned long long x = 0;
static byte bits[64];

do {
if (Int2Bin(t++, bits) == 22) {
Count++;
if (t > x) {
x = t + 0x40000;
printf("%s :: Count = %d\n", bits, Count);
}
}
} while (t < 0x7FFFFE00000);

printf("\nTest program completed successfully.\n\n");
return 0;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
Bit more DOF, bit more light, bit better? Dudley Hanks Digital Photography 9 03-29-2009 09:46 PM
How much time does it need to sort 1 million random 64-bit/32-bit integers? Weng Tianxiang VHDL 36 07-15-2006 04:34 AM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57