Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: long long in C++ Berlin


This question is answered.


Permlink Replies: 4 - Last Post: Oct 21, 2016 4:02 PM Last Post By: Remy Lebeau (Te...
Earl Staley

Posts: 99
Registered: 4/9/07
long long in C++ Berlin  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2016 11:27 AM
I am trying to use a long long datatype in C++ Berlin, but the resulting number wraps at 4 bytes rather than 8 bytes. I have several declarations:

long long number = 0;
void RetrieveGenre(long long GenreNo);
bool IsBitSet(long long Numb, int BitNo);
long long SetBit(long long Numb, int bit, bool SetTo);

The value of 'number' is passed to the functions in the 'Numb' and 'GenreNo' variables. I need 39 bits in the current program and a long long should have 8 bytes=64 bits (63 + sign bit) to work with.

Is there a way to make a long long datatype (64 bits) in C++ Berlin?

Thank you...
Earl Staley

Edited by: Earl Staley on Oct 21, 2016 12:25 PM
Earl Staley

Posts: 99
Registered: 4/9/07
Re: long long in C++ Berlin  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2016 12:40 PM   in response to: Earl Staley in response to: Earl Staley
I did some more troubleshooting and the long long datatype is 8 bytes in C++ Berlin, as it should be. However my code is failing here when the value of 'bit' >=32.

long long TFormMainDVD::SetBit(long long Numb, int bit, bool SetTo) {
	if (SetTo == true) {
		return Numb |= 1 << bit;
	}
	else {
		return Numb &= ~(1 << bit);
	}
}

Any ideas why? Shouldn't I be able to enter 'bit' values up to 62 and have it work correctly?

Regards...
Earl

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: long long in C++ Berlin
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2016 1:33 PM   in response to: Earl Staley in response to: Earl Staley
Earl wrote:

However my code is failing here when the value of 'bit' >=32.
Any ideas why?

You are bit-shifting a 32bit value instead of a 64bit value, so the shift
will overflow. You need to type-cast the 1 before shifting it. I would
also suggest you use __int64 or int64_t instead of long long:

bool IsBitSet(__int64 Numb, int BitNo) {
    return (Numb & (__int64(1) << bit) != 0);
}
 
__int64 TFormMainDVD::SetBit(__int64 Numb, int bit, bool SetTo) {
    __int64 mask = __int64(1) << bit;
    if (SetTo) {
        return Numb | mask;
    }
    else {
        return Numb & ~mask;
    }
}


--
Remy Lebeau (TeamB)
Earl Staley

Posts: 99
Registered: 4/9/07
Re: long long in C++ Berlin  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2016 2:38 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy,

Thank you for the help. I now have have one other problem. The value of 'number', which is now 64 bits, is being stored in an SQLite database field of type integer.

	DataModule1->FDQueryAV2->FieldByName("Genre")->Value = number;


Per the sqlite.org documentation, integers are stored as follows:

"INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value."

From the above, I assume that SQLite can handle an 8 byte integer. However, when the program attempts to store the number, which is of type 'int64_t', I get these errors:

"Range check error"

"Overflow while converting variant of type (Uint64) into type (Integer)"

"Invalid value for field 'Genre'"

Is there a way to store the 'int64_t' value in the field 'Genre'?

Thank you...
Earl Staley
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: long long in C++ Berlin
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2016 4:02 PM   in response to: Earl Staley in response to: Earl Staley
Earl wrote:

However, when the program attempts to store the number, which is
of type 'int64_t', I get these errors:

"Range check error"

"Overflow while converting variant of type (Uint64) into type
(Integer)"

"Invalid value for field 'Genre'"

That can happen if FieldByName() returns a TIntegerField object instead of
another object type, like TLargeintField, that can handle 64bit numbers.
The FireDAC driver you are using to connect to SQLite tells TFDQuery the
data type used for each field so it can create suitable TField objects.
Maybe you are using a buggy FireDAC driver. Or maybe SQLite is reporting
the field as INTEGER instead of (UNSIGNED) BIGINT, for instance.

Embarcadero's DocWiki shows how SQLite data types are mapped to FireDAC data
types:

http://docwiki.embarcadero.com/RADStudio/en/SQLite_support_in_RAD_Studio

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02