fastest way to NOT a binary sequence in decimal form

This is a discussion on fastest way to NOT a binary sequence in decimal form within the C Programming forums, part of the General Programming Boards category; I have a decimal, say 8, which is 1000 in binary. What I want to do is to NOT the ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    8

    fastest way to NOT a binary sequence in decimal form

    I have a decimal, say 8, which is 1000 in binary. What I want to do is to NOT the sequence so it becomes 0111 and then get 7. Now the manual way is to just convert 8 to binary and then XOR with 1111 to get 0111. Then finally converting 0111 to 7. I was wondering if there was a way to skip the conversion and directly go from 8 to 7?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Inverting ~8 will be fastest due to underlying hardware support for such operations.
    Last edited by itCbitC; 09-13-2011 at 12:40 PM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Code:
    $ cat foo.c
    #include <stdio.h>
    int main ( ) {
      int a = 8;
      printf("%d\n",(~a)&0x0F );
      return 0;
    }
    $ gcc foo.c
    $ ./a.out 
    7
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,306
    I think part of your confusion is this concept of "conversion". The computer only deals in binary. When it shows you a decimal number, it's showing you the decimal representation of the underlying binary data. When you type in a decimal number, the functions like scanf and strtol convert that text to a number. That number is stored in binary. All those decimal numbers you type when programming get converted to binary when the compiler generates code. Also, as Salem pointed out, C has a bitwise NOT operator, the ~. It's equivalent to XORing with all 1's, but it's clearer and as fast or faster.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    how would i get it working with 8 bit numbers? the not operator works great, but seems like 0x0f only deals with the 4bit to the right most.
    Last edited by tianshiz; 09-13-2011 at 01:17 PM.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    thanks

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,306
    Quote Originally Posted by tianshiz View Post
    how would i get it working with 8 bit numbers? the not operator works great, but seems like 0x0f only deals with the 4bit to the right most.
    Well, your examples only dealt with the rightmost nibble (4-bit chunk), so naturally Salem's suggestion did likewise. The ~ operator will work with signed and unsigned versions of char as well as short, int, long, long long. Just declare an 8-bit variable (char or unsigned char), and use the ~ without the & 0x0f bit.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tianshiz View Post
    how would i get it working with 8 bit numbers? the not operator works great, but seems like 0x0f only deals with the 4bit to the right most.
    Code:
    #include <stdio.h>
    
    int main (void)
      { unsigned int x  =  0;
    
         printf("%d  %d", x, ~x);
    
         return 0; }
    You can always do little tests like this... it's a great way to learn what does what.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary sequence in lexicographic order
    By confuser008 in forum C++ Programming
    Replies: 5
    Last Post: 10-16-2009, 01:10 PM
  2. Converting seconds to hours in decimal form?
    By ajmcello in forum C Programming
    Replies: 3
    Last Post: 04-24-2006, 02:35 AM
  3. Binary Sequence
    By royshh in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2006, 01:56 PM
  4. fastest way to copy binary files...
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 06-24-2002, 12:55 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 10:07 PM

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