hello
I use miracle library to work with big numbers.
I defined a number for example X with 192 bit. at first X= 0 ;
I want to change for example 3 bit of it to 1 randomly.
as a example:
assume that X has 10 bit :
X=0000000000
random numbers should be :
1000010001
1010100000
1100000100
...
...
how can I do it ?
best Regards
:)
How looks the intern representation of such a big number?
Array of unsigned int's or string or what?
big type is like int,
big x=0;
I can't access to each bit of it like an array. but i can represent it in all of radix
you can assume that you want change the bit of integer number.
Quote from: zahrasaffar on January 01, 2015, 02:29:08 PM
big type is like int,
Impossible!
Please give us the definition of 'big'.
I just have this file from miracl library . I don't Know :(
if I want to do this with an integer value how can I do it ?
maybe it can help me and I should do the same for big type .
Quote from: zahrasaffar on January 01, 2015, 02:51:38 PM
I just have this file from miracl library . I don't Know :(
Read page 15 ff.
Quote from: czerny on January 01, 2015, 04:45:31 PM
Quote from: zahrasaffar on January 01, 2015, 02:51:38 PM
I just have this file from miracl library . I don't Know :(
Read page 15 ff.
OK . thanks for your attention . But I'm a beginner in programming . How can I make these random numbers ?
first I want to produce for example 100 192bit-numbers with 3 bit 1. after that 4 bit 1 ,next 5 bit 1 and so on . how can I produce these numbers ?
Hope I can help a bit. As a beginner, I strongly suggest you start with smaller numbers. The largest 'int' defined in Pelles C that I found is 'long long' which is 64 bits. How the number is used (signed or unsigned) is more a matter of the function you are using. So, doing normal math with interpret a '1' in the most significant bit as a minus sign. (Look up 'two's complement' on Wikipedia for details.)
So, if you want to work with numbers that are bigger than what is native to the compiler, you will need to write your own functions. Consider the fact that two 16-bit numbers can represent a 32-bit number if put end to end. Do some basic research on binary math and you can fabricate a function that works with multiple numbers. So the first number may be composed of 'a_lowbyte' and 'a_highbyte'. The second number may be 'b_lowbyte' and b_highbyte'. Start on paper and figure out how to add these together to get 'c_lowbyte' and c_highbyte'.
From there, bit manipulation can follow. You may ultimately use a 'structure' to hold the data sets, but start at the beginning and play, play, play.
It's hilarious that C doesn't provide native functions to display numbers in binary form, or to change single bits.
Here is a solution that uses inline assembly to provide:
- bin$(*number, size)
. bchg(*Number, bitpos)
The examples are for integers and long longs but both function should work for integers of any size (test yourself - the usual disclaimers apply).
#include <stdio.h> // display numbers in binary form, and manipulate single bits
#pragma warn(disable:2007) // assembly not portable
char buffer [1000]; // could be a big number ;-)
char* bin$(int Number, int ct) {
_asm {
push esi
push edi
mov esi, Number
mov edi, offset buffer
mov ecx, ct
lea esi, [esi+ecx]
shl ecx, 3 ; bytes to bits
L0:
test ecx, 31
jne L1
mov al, 32
stosb
sub esi, 4
mov eax, [esi]
xchg eax, edx
L1:
shl edx, 1
setc al
add al, 48
stosb
dec ecx
ja L0
xor eax, eax
stosd
pop edi
pop esi
}
return buffer;
}
void bchg (int Number, int bitpos) {
_asm {
push esi
mov esi, Number
mov ecx, bitpos
mov edx, ecx
shr edx, 3 ; bits to bytes
add esi, edx
lodsd ; get bits
shl edx, 3
sub ecx, edx ; get bitpos
ror eax, cl
btc eax, 0
rol eax, cl
mov [esi-4], eax
pop esi
}
}
int main(void) {
unsigned long long MyLL=0x88AACCEE99ABCDEF;
unsigned int MyInt=0x88AACCEE;
int pNumber;
pNumber=(int)&MyLL;
printf("The number has %i bytes and %i bits\n", sizeof(MyLL), sizeof(MyLL) * 8 );
printf("binary: %s\n", bin$(pNumber, sizeof(MyLL)));
printf("expected: %s\n\n", "10001000101010101100110011101110 10011001101010111100110111101111");
bchg(pNumber, 2); // change the third-last bit
bchg(pNumber, sizeof(MyLL)*8-3); // change the third bit
printf("binary: %s\n", bin$(pNumber, sizeof(MyLL)));
printf("expected: %s\n\n", "10101000101010101100110011101110 10011001101010111100110111101011");
pNumber=(int)&MyInt;
printf("The number has %i bytes and %i bits\n", sizeof(MyInt), sizeof(MyInt) * 8 );
printf("binary: %s\n", bin$(pNumber, sizeof(MyInt)));
printf("expected: %s\n\n", "10001000101010101100110011101110");
bchg(pNumber, 2);
bchg(pNumber, sizeof(MyInt)*8-3);
printf("binary: %s\n", bin$(pNumber, sizeof(MyInt)));
printf("expected: %s\n\n", "10101000101010101100110011101010");
}
Output:
The number has 8 bytes and 64 bits
binary: 10001000101010101100110011101110 10011001101010111100110111101111
expected: 10001000101010101100110011101110 10011001101010111100110111101111
binary: 10101000101010101100110011101110 10011001101010111100110111101011
expected: 10101000101010101100110011101110 10011001101010111100110111101011
The number has 4 bytes and 32 bits
binary: 10001000101010101100110011101110
expected: 10001000101010101100110011101110
binary: 10101000101010101100110011101010
expected: 10101000101010101100110011101010
Quote from: jj2007 on January 04, 2015, 06:59:11 AM
It's hilarious that C doesn't provide native functions to display numbers in binary form, or to change single bits.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char s[100];
printf("%s\n", _itoa(0xFFFFFFF, s, 2));
return 0;
}
<intrin.h>
unsigned char _bittestandreset(long const *valueptr, long index);
unsigned char _bittestandreset64(long long const *valueptr, long long index);
unsigned char _bittestandset(long const *valueptr, long index);
unsigned char _bittestandset64(long long const *valueptr, long long index);
#define set_bit(arr,x) ((arr[(x)>>3]) |= (0x01 << ((x) & 0x07)))
#define clear_bit(arr,x) (arr[(x)>>3] &= ~(0x01 << ((x) & 0x07)))
#define get_bit(arr,x) (((arr[(x)>>3]) & (0x01 << ((x) & 0x07))) != 0)
Quote from: jj2007 on January 04, 2015, 06:59:11 AM
It's hilarious that C doesn't provide native functions to display numbers in binary form, or to change single bits.
Pelle's C has
_itoa, _ltoa, _ultoa, _itow, _ltow and _ultow.
Microsoft has
_itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow
as well as
_itoa_s, _i64toa_s, _ui64toa_s, _itow_s, _i64tow_s, _ui64tow_s
For details see
http://msdn.microsoft.com/en-us/library/yakksftt.aspx
thanks every body . I think may be I couldn't explain my problem in a good way . I don't have any problem with big numbers . Miracle has this type . When I use Miracle I can define my number as fallowing :
big x;
then I read the value of X from a file.I give the value as a Hex-number. first my value number is :
X=000000000000000000000000000000000000000000000000 H ----> 48 digits in Hex
at first I can produce the numbers that these 3 bits are besides together as fallowing:
give this value as a initiate number to X ----> E00000000000000000000000000000000000000000000000
next I use Bitwise operation and shift the numbers to right . so it produce for me all the 3bit-numbers that 3 bit 1 are besides together .
But now I want to produce for example 1000 random numbers with 3 bit of 1 .and the locations of 1bits is not important for me .the numbers just should have 3 bit 1. how can I do it ?
I hope I could explain for you what is in my mind ;)
thanks for your patience :)
Quote from: Robert on January 04, 2015, 10:43:33 AMPelle's C has
_itoa, _ltoa, _ultoa, _itow, _ltow and _ultow.
Timo & Robert,
Apologies :)
I swear I googled quite a bit last night, and stumbled over hilarious stuff. So it is that simple:
_itoa(MyInt, buffer, 2);
printf("binary: %s\n", buffer);
Nonetheless, I hope the code above may serve as a substitute for _i64toa(). Besides, it will work for any size, i.e. for "big" integers, too.
I haven't used the MIRACL library but a quick search looks interesting. Traditionally, one sets and clears bits using AND (to clear); OR (to set); XOR (to toggle). The concept is, to set bit 3 in 'big_nmbr' ...
big_nmber = big_nmber | 0x08; // Hope I'm doing this right
To clear the number use ...
big_nmber = big_nmber & ~0x08; // The '~' operator complements (inverts) all bits
BUT... unless MIRACL has these functions, they won't work on the numbers created by MIRACL. Search the header file and see if you can find functions that will accomplish what you need.