Reverse of a Binary

This is a discussion on Reverse of a Binary within the C++ Programming forums, part of the General Programming Boards category; Can anyone help and let me know how can I reverse a Binary number, please ASAP. What I mean is ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Unhappy Reverse of a Binary

    Can anyone help and let me know how can I reverse a Binary number, please ASAP.
    What I mean is if I get 00000001 00000010 00000010 I should get as a result: 10000000 01000000 01000000.
    void reverse_bit(unsigned char x, char *pt)
    {
    int t, i;
    unsigned char hold;
    char temp[8];
    char tmp_bin[8];
    char *pt2=0;

    tmp_bin[0]='\0';
    pt2 = tmp_bin;
    display_binary(x, pt2);


    hold = x;

    for(t=1; t <count; t++)
    {
    if(hold & HIGH_BIT)
    hold = ( hold << 1 | 1);
    else
    hold = hold << 1;
    display_binary(hold, pt2);
    }
    x = hold;
    strcpy(temp,"");
    for(i = HIGH_BIT; i > 0; i = i/2)
    { if(x & i)
    sprintf(temp, "%s1", temp);
    else
    sprintf(temp, "%s0", temp);
    }

    strcpy(pt,temp);}
    this is what i did and it does not work.
    Last edited by xeneize; 10-07-2002 at 12:30 PM.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    Is it homework or exam ??

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    one way would be to store each 8 char grouping as a separate string, then reverse the string with one of the string reverse functions (depending what type of string you use) or derive your own string reverse function if that is the point of the exercise.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Can anyone help and let me know how can I reverse a Binary number, please ASAP.
    Why? Written a decimal to binary conversion badly??
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  2. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21