Thread: Bit Operations & Math

  1. #1
    Registered User
    Join Date
    Nov 2014
    Location
    Centurion, Gauteng, South Africa
    Posts
    28

    Bit Operations & Math

    I was just curious. I thought i'd go ahead and learn some assembly. During that time I saw a trick to figure out a characters case ( the third bit of a sequence indicates the case of a character ) so I thought I'd implement it in C before trying it in Nasm.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int IsUpper(char AChar); //Will return 1 for any uppercase char.
    int IsUpper(char AChar)
    {
        return (0b00100000 & AChar) | 0b1;
    }
    int main()
    {
        printf("%i",26 >> 1); //shl 2 is equal to * by 4
        return 0;
    }
    The IsUpper function works fine Is it at it's fastest though? Is it correct? I mean the | 0b1 is the same as +1 right? Only the program doesn't have to do the conversion because it's already in binary?

    As for the 26 >> 1 , I just remembered that from some delphi challenge ages ago. That's the same as dividing an even integer with two correct? I get an error if i try %f and 26.0 anyway. I was just curious if 26 shr 1 is faster tha 26 / 2 .

    Just for the sake of it ^^

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > The IsUpper function works fine Is it at it's fastest though? Is it correct?
    I dunno. Why don't you write
    for ( int ch = 0 ; ch < 256 ; ch++ )
    and test every possible char?

    FWIW, these bit-tricks only work on ASCII character sets. It won't work for example on EBCDIC.

    > I mean the | 0b1 is the same as +1 right?
    Try it with an odd number.

    > I was just curious if 26 shr 1 is faster tha 26 / 2 .
    Nowadays, only the most brain-dead of compilers is incapable of optimising /2 as a shift instruction.
    Do yourself a favour and stop searching for micro-optimisations.

    A modern compiler has the distilled wisdom of probably 1000's of years of expertise within it. Just once, you might get a very localised 'win' against it. The other 99.99% of the time, you're just going to waste your time.

    For instance, x >> n can give you a big surprise on some machines if x is negative.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2014
    Location
    Centurion, Gauteng, South Africa
    Posts
    28
    >> I mean the | 0b1 is the same as +1 right?
    >Try it with an odd number.

    Sorry didn't state my question correctly , I realise the result would be different because an odd number in binary would have a 2 ** 0 which would result in a 1 being carried etc etc. Thanks for pointing that out though , It'll probably help me remember that :/

    I'm not really searching for micro optimistations more than I am trying to gain understanding I'm also making a point of learning nasm (general assembly ?? ) because I believe I'll require it in the future.

    I'll test it in a for loop right after this post . I assumed the output would be fine because the logic behind it seems solid ^^ Then again I didn't really study any of this so your probably right with the testing.

    Code:
    char AChar;
        for(AChar='A';AChar-1<'Z';AChar++)
            printf("Testing %c : %i\n",AChar,(0b00100000 & AChar) | 0b1);
    I tested it with all the uppercase characters (I mean the alphabet letters , i never intended to test it with them all XD ) and it works fine I'll just make a comment next to the function about the EBCDIC format.
    Not that I think I'd ever use an IBM pc but all knowledge is good right?

    As always though I find your replies informing and helpful !
    Thanks Salem
    Last edited by RagingGrim; 12-23-2014 at 01:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. math function (order of operations)
    By slee8251 in forum C Programming
    Replies: 3
    Last Post: 02-03-2012, 10:03 AM
  2. Trouble with math operations-Beginner program
    By brewgrass in forum C++ Programming
    Replies: 11
    Last Post: 08-08-2011, 03:47 AM
  3. Math Operations in a Variable?
    By MPQC in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2009, 01:32 PM
  4. doing floating operations using integer operations
    By ammalik in forum C Programming
    Replies: 10
    Last Post: 08-15-2006, 04:30 AM
  5. Question involving using math operations...
    By Screwz Luse in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 05:20 PM

Tags for this Thread