Results 1 to 21 of 21

Thread: Need help with creating Binary Class

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    4

    Need help with creating Binary Class

    I am trying to create a class to manipulate binary number including +, -, & * functions. here is what i already have.

    Code:
    #include <iomanip.h>
    #include <stdio.h>
    #include <iostream.h>
    #include <stdlib.h>
    
    class bin_num{
       char data[8];
       int todec(bin_num x);
       bin_num tobin(int x);
       bin_num operator +(bum_num x);
       bin_num operator -(bum_num x);
       bin_num operator *(bum_num x);}
    
    bin_num::int todec(bin_num x){
       int y;
       y = strtol(x, NULL, 2);
       return y;
    }
    
    bin_num::bin_num tobin(int x){
       char bnum[8];
       itoa(x, bnum, 2); 
       return bnum;
    }
    
    bin_num::bin_num operator +(bum_num x){
    }
    
    bin_num::bin_num operator -(bum_num x){
    }
    
    bin_num::bin_num operator *(bum_num x){
    }
    i don't quite know what the operator functions should be. If anyone could help, PLEASE!!!!
    Attached Files Attached Files

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I find some things in your code strange (except that you mispelled bin_num in all arguments to the operators).
    todec
    why does this take an argument? Do you want to convert any bin_num to an int? If so, make it static. If not, don't take an argument and return the int value of the string in you class. I wouldn't do that as a function, but rather as a cast operator:
    Code:
    operator int ();
    This doesn't take an argument, nor has it an explicit return type. The return type is defined by the type it casts to.
    tobin
    same thing: either static, or assign value to object. You can return the value if you want (but NOT a local array as you did!) You also maybe want to rename the function to
    bin_num(int i);
    without return type. This is called a cast constructor and enables you to write this:
    Code:
    int i = 34;
    bin_num bn = (bin_num)i;
    In addition, you could write an operator char*, that returns the string associated with the object.

    Don't forget the public keyword, at least for the functions (it's not a bad thing to hide the string)

    I think an int can be up to about 16 billions (or was it even more?), so you'll need a longer string. (10 maybe?)

    The operators:
    Basically you have to convert both operands to ints, perform the operation and convert the result back to a bin_num.
    For example:
    Code:
    void bin_num::operator +=(const bin_num &num)
    {
         int iThis, iNum, iResult;
         iThis = atoi(data);
         iNum = atoi((char*)num);  // requires cast operator
         iResult = iThis + iNum;
         itoa(data, iResult);
    }
    Note that this was the += operator, not the + operator, which is usually a global function:
    Code:
    bin_num operator +(bin_num & num1, bin_num & num2)
    {
         bin_num temp;    // the constructor should set the int-value to 0
         temp += num1;
         temp += num2;
         return temp;
    }
    If data is private (should be), you have to declare this function a friend of the class.

    You can also write + operators for types like int and char*. You may even want to make it UNICODE compatible by using TCHAR and the _tcs function family and _ttoi or __itot.

    One more thing:
    Make sure you declare inline functions in the header file of the class (or a subinclude) and normal functions in the class module.

    If you don't know any of the mentioned techniques, ask me.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Why do u use the bin_num:: namespace??

    your code should look like:
    Code:
    #include <iomanip.h>
    #include <stdio.h>
    #include <iostream.h>
    #include <stdlib.h>
    
    class bin_num{
       char data[8];
       int todec(bin_num x);
       bin_num tobin(int x);
       bin_num operator +(bin_num x);
       bin_num operator -(bin_num x);
       bin_num operator *(bin_num x);}
    
    int bin_num::todec(bin_num x){
       int y;
       y = strtol(x, NULL, 2);
       return y;
    }
    
    bin_num bin_num::tobin(int x){
       char bnum[8];
       itoa(x, bnum, 2); 
       return bnum;
    }
    
    bin_num bin_num::operator +(bin_num x){
    }
    
    bin_num bin_num::operator -(bun_num x){
    }
    
    bin_num bin_num::operator *(bun_num x){
    }
    I just had to ask, does this code even compile?? i mean u have so many syntax errors, nevermind code errors... but syntax errors?

    Well have you tried:
    Code:
    cout << bin << theNum << num; // change it back to num when done
    its that simple.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    4

    ???

    this is the first time i had to use a class. i know it will now compile. that is why i need help

  5. #5
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Oh sorry, i think i might've mislead you somehow... you have some mistakes in ur code such as bum_num and all of ur class definition syntax seems to be messed up.

    It is supposed to be:
    Code:
    <return type> <scope (usually classname followed by two colons ::)><function name> ...
    Going back to the problem, instead of having tobin and todec functions, it would be best that you overload the following casts:

    (int)

    also, make the length of data to be 64, because if you are going to convert any int to bin, then you need to be prepared.

    Anyways, here is some code:

    Code:
    // Here are a few more additions to what CornBee started.
    void bin_num::operator -=(const bin_num &num)
    {
         int iThis, iNum, iResult;
         iThis = atoi(data);
         iNum = atoi((char*)num);  // requires cast operator
         iResult = iThis - iNum;
         itoa(data, iResult);
    }
    
    bin_num operator -(bin_num & num1, bin_num & num2)
    {
         bin_num temp;
         temp += num1; // first add the first number and then subtract that from the second
         // like this: num1 - num2
         temp -= num2;
         return temp;
    }
    
    void bin_num::operator *=(const bin_num &num)
    {
         int iThis, iNum, iResult;
         iThis = atoi(data);
         iNum = atoi((char*)num);  // requires cast operator
         iResult = iThis * iNum;
         itoa(data, iResult);
    }
    
    bin_num operator *(bin_num & num1, bin_num & num2)
    {
         bin_num temp;
         temp += num1; // first add the first number and then multiply that from the second
         // like this: num1 * num2
         temp *= num2;
         return temp;
    }
    
    // and here's an overload of char*
    char* bin_num::operator char*(bin_num& num)
    {
         return &num.data; // return a pointer to the internal data (which is already a char)
    }
    And thats all for today, maybe I can write up more for u later.

    Good luck rrrct31.
    -MoMad
    Last edited by MoMad; Sep 19th, 2001 at 02:22 PM.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    hehe, disable smilies
    class:perator
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Smile whats the point

    in having a binary number stored as a string when binary is the format that data will end up in your memory anyway?

    a regular char would be much much more efficient.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    4

    to answer your question

    if you are making an encryption program, you have to be able to manip the binary code to change the numbers.

    ex: (using ASCII vaules)

    this is the method used by the CIA

    to encrypt the string "abc" first you have to convert it to binary (assuming you don't already have it in binary)
    Code:
       a        b        c
    01100001 01100010 01100011
    then you have to add the secrete code. in this case it is 101. assuming 0 + 0 = 0, 1 + 0 = 1, and 1 + 1 = 0 (not 10)
    Code:
       a        b        c
    01100001 01100010 01100011 and then coverting 
    10110110 11011011 01101101 them back to ASCII
    -------------------------- \/\/\/\/\/\/\/\/\/
    11010111 10111001 00001110         |
     hidden   hidden   hidden  <-------+
    it returns 3 hidden chars that are only visible as little blocks or blank space to normal people.

    that is why storing binary numbers this way is
    more efficient than a single char

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Re: to answer your question

    First off, all integers are stored binary, and processed binary as well, by the cpu. So any number represent a binary value and any operations performed on it is done binary. Naturally this is also the both most performance and memory efficient way.
    then you have to add the secrete code. in this case it is 101. assuming 0 + 0 = 0, 1 + 0 = 1, and 1 + 1 = 0 (not 10)
    You should use bitwise exclusive or (^) operator on integers. It returns bitwise inequality "!=".

    a simple sample:
    Code:
     01100001 (97)
    ^10110110 (182)
    -------------
     11010111 (215)
    cout << (97 ^ 182);

    says "215"
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    simple sample on xor encryption

    most efficient would be to xor 32 bit integers, that means 32 bits in a single cpu instruction, not several per bit on a string

    PHP Code:
        char a[25]="This is a test string!!";
        
    int key =12345678;

        for (
    intb=(int*)a;b<(int*)(a+sizeof(a));*b++^=key);

        
    cout << << endl;

        for (
    b=(int*)a;b<(int*)(a+sizeof(a));*b++^=key);

        
    cout << << endl
    What i do here is take a char string and xor it against a 32 bit key "12345678". Then xor it back. If *b++^=key looks confusing it is the same as
    *b^=key; // xor assign key on itself
    b++; //goto next integer
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Humm, somehow I was wondering why anyone would want to convert an int into a bin when they are essentially the same, but when you're trying to view the number in binary, i think there was a built in C/C++ operand that outputs and.or displays binary representation of a number.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by MoMad
    Humm, somehow I was wondering why anyone would want to convert an int into a bin when they are essentially the same, but when you're trying to view the number in binary, i think there was a built in C/C++ operand that outputs and.or displays binary representation of a number.
    You mean _itoa? it's a function... what's a bin btw, there's no bin datatype in C++
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  13. #13
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    sorry bin = binary number,
    and why do u use the functions with underscore? arent they supposedly to be private functions?
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by MoMad
    sorry bin = binary number,
    and why do u use the functions with underscore? arent they supposedly to be private functions?
    itoa doesn't work for me, I need to use the underscore, I have no idea why.

    I don't think issue is to view the binary number
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  15. #15
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    oh ok.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  16. #16
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    hey kadaman, i think you'd have to use #include <stdlib.h>
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  17. #17
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    why is that? iostream.h should do
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  18. #18
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    That could be a reason you HAVE to use the function with underscores.... those functions are ANSI-C. So I have no idea why they dont work for you... try using #include <stdlib.h> and see if it works without the _, and see which is faster, etc
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  19. #19
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Okay that seems to work, although i noticed that if i have <iostream> instead of <iostream.h> then i can use itoa as well, thats odd
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  20. #20
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    aww, u should've told me u had it WITHOUT the 'h'. If you dont have the .h, then you are using the NAMESPACE!! it is very different than using the header that is why.

    You could've used it like:
    PHP Code:
    #include <iostream>
    using namespace STD// i forgot the exact syntax

    // now you can call the ito blah blah whatever.
    itoa(x,r); // compiles 
    I think... it was something like that... anyways, its getting late and im just gonna stop debuggin for now and call it a night.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  21. #21
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    well, for me, it works without using using namespace std as well
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width