473,587 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tell me more about 64 bit integers

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.
Nov 7 '06 #1
5 2040
On Tue, 07 Nov 2006 14:39:43 -0600, Kenneth Lantrip
<bo********@cma access.comwrote 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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 8 '06 #2

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"...

Nov 8 '06 #3
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.

Nov 9 '06 #4
xs*****@gmail.c om 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>
Nov 9 '06 #5
xs*****@gmail.c om 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(unsigne d 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;
}
Nov 9 '06 #6

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

Similar topics

303
17539
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which...
22
2303
by: bearophile | last post by:
Ville Vainio: >It's highly typical for the newbies to suggest improvements to the >language. They will usually learn that they are wrong, but the >discussion that ensues can be fruitfull anyway :-). Few more notes on the language. I don't know if I can really suggest improvements to the language... but I hope to learn something :-) I...
29
7456
by: Chris Dutrow | last post by:
I searched around on the net for a bit, couldn't find anything though. I would like to find some code for a function where I input A Range Of Integers For example: Function( 1, 100 ); And the function will return me an array holding a random subset of integers in that range of a size that I specify So the Function would Probabaly look...
6
1698
by: Markus Dehmann | last post by:
I have n sets of elements. I want to find elements that occur more than once in more than one set. Maybe the following example shows what I mean: S1 = {1,2,3,2,4} S2 = {2,2,4,5,4} S2 = {2,5,2} The algorithm should find that the "2" occurs more than once in S1, S2, and
5
1549
by: greenflame | last post by:
How can I tell if a variable. I want ot tell whether two variables are: 1. Both numbers. Just a number. same as the number object in javascript. 2. Either one is a string containing a number. For exmaple a string like "354" or "295.395". 3. Either are strings containing a fraction. For example a string like "46/987". 4. Either is a...
39
2794
by: Antoon Pardon | last post by:
I was wondering how people would feel if the cmp function and the __cmp__ method would be a bit more generalised. The problem now is that the cmp protocol has no way to indicate two objects are incomparable, they are not equal but neither is one less or greater than the other. So I thought that either cmp could return None in this case...
16
3377
by: aruna | last post by:
Given a set of integers, how to write a program in C to sort these set of integers using C, given the following conditions a. Do not use arrays b. Do not use any comparison function like if/then or switch-case c. you can use pointers only d. you cannot use any of the loops either.
161
7790
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.. (After reading that
8
9466
by: bearophileHUGS | last post by:
sys.maxint gives the largest positive integer supported by Python's regular integer type. But maybe such attribute, with few others (they can be called min and max) can be given to int type itself. D is a very nice language, that I hope to see more used. It is copying lot of things from Python. D Floating point values have some proprieties: ...
0
7923
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...
0
7852
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8349
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...
0
8221
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...
1
5719
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5395
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...
0
3845
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...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.