NO

Author Topic: suggestions for changing some bit of a number  (Read 7150 times)

zahrasaffar

  • Guest
suggestions for changing some bit of a number
« on: January 01, 2015, 01:13:49 PM »
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
 :)

czerny

  • Guest
Re: suggestions for changing some bit of a number
« Reply #1 on: January 01, 2015, 01:40:48 PM »
How looks the intern representation of such a big number?
Array of unsigned int's or string or what?

zahrasaffar

  • Guest
Re: suggestions for changing some bit of a number
« Reply #2 on: January 01, 2015, 02:29:08 PM »
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.
« Last Edit: January 01, 2015, 02:31:07 PM by zahrasaffar »

czerny

  • Guest
Re: suggestions for changing some bit of a number
« Reply #3 on: January 01, 2015, 02:39:10 PM »
big type is like int,
Impossible!

Please give us the definition of 'big'.

zahrasaffar

  • Guest
Re: suggestions for changing some bit of a number
« Reply #4 on: January 01, 2015, 02:51:38 PM »
I just have this file from miracl library . I don't Know  :(

zahrasaffar

  • Guest
Re: suggestions for changing some bit of a number
« Reply #5 on: January 01, 2015, 02:56:33 PM »
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 .
 

czerny

  • Guest
Re: suggestions for changing some bit of a number
« Reply #6 on: January 01, 2015, 04:45:31 PM »
I just have this file from miracl library . I don't Know  :(
Read page 15 ff.

zahrasaffar

  • Guest
Re: suggestions for changing some bit of a number
« Reply #7 on: January 02, 2015, 04:02:58 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 ? 

jrockel55

  • Guest
Re: suggestions for changing some bit of a number
« Reply #8 on: January 03, 2015, 06:37:26 PM »
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.

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: suggestions for changing some bit of a number
« Reply #9 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.
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

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: suggestions for changing some bit of a number
« Reply #10 on: January 04, 2015, 10:34:51 AM »
It's hilarious that C doesn't provide native functions to display numbers in binary form, or to change single bits.
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
char s[100];
printf("%s\n", _itoa(0xFFFFFFF, s, 2));
return 0;
}
Code: [Select]
<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);
Code: [Select]
#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)
May the source be with you

Offline Robert

  • Member
  • *
  • Posts: 247
Re: suggestions for changing some bit of a number
« Reply #11 on: January 04, 2015, 10:43:33 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

zahrasaffar

  • Guest
Re: suggestions for changing some bit of a number
« Reply #12 on: January 04, 2015, 05:41:34 PM »
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  :)
 

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: suggestions for changing some bit of a number
« Reply #13 on: January 04, 2015, 05:56:12 PM »
Pelle'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:
Code: [Select]
  _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.

jrockel55

  • Guest
Re: suggestions for changing some bit of a number
« Reply #14 on: January 05, 2015, 12:29:38 AM »
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.