function help needed

This is a discussion on function help needed within the C Programming forums, part of the General Programming Boards category; hi! ive stumbled across this function heres the code Code: int rev(int a){ int i=1; if((*(char*)&i)) return(a); return((a>>24)&0xff)|(((a>>16)&0xff)<<8)|(((a>>8)&0xff)<<16)|((a&0xff)<<24); } of ...

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    function help needed

    hi!
    ive stumbled across this function
    heres the code
    Code:
    int rev(int a){
        int i=1;
        if((*(char*)&i)) return(a);
        return((a>>24)&0xff)|(((a>>16)&0xff)<<8)|(((a>>8)&0xff)<<16)|((a&0xff)<<24);
    }
    of special interest is the second return statement.
    the variable a is shifted right by certain values.
    the result is masked with 1111 1111. so everywhere where a 1 is in the binary notation of the result a one will be set for that statement.
    this result (either 1 or 0) is again ORed with a similar statement.

    is all that correc?
    well and what is that used for?


    thanks alot!

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    My guess is that it is meant to reverse the order of the 8-bit bytes in the input int. So rev(0x12345678) is meant to return 0x78563412. This is spoiled by the over complex and presumably incorrect if statement that returns a if a != 0. Thus as is stands, the function just returns whatever you entered into it. Remove this line and everything starts to work more sensibly!

    ::edit::
    beaten to it by Salem... and I missed the whole endian thing... I shall hang my head in shame
    Last edited by DavT; 07-09-2003 at 04:10 AM.
    DavT
    -----------------------------------------------

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    creating bitmasks

    thanks for your replies.

    i have implemented my own very basic XOR encryption code.
    as usual it encrypts the given ascii text with a key specified by the user.
    well.. now i want to tell the user what binary value his original had and what the encrypted text has.

    i made a very common approach in creating a bitmask to mask all the bits but not the least significant bit.
    then shifting the binary value to the right by one each time would bring me the desired result.

    very often i see bitmasks being hexadecimal values like 0xff (1111 1111), or octal ones.
    but how would i create my bitmask? in binary it should look like that: 0000 0001.
    and how would i create in general my own bitmasks being something special like for example 0100 1110?

    is it enough to know how the hexadecimal numbering system works?

    thanks alot!

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    kewl.
    thanks .

  5. #5
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    heres my samplecode
    it doesnt seem to work. its giving me zeroes everywhere in the binary number.
    Code:
    #include <stdio.h>
    
    int main (void)
    {
        unsigned int a = 41;
        unsigned char mask = 0x8000;
        int i = 8;
        printf("unsigned int a = 41\n");
        printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
        printf("now testing from the msb on if bit is set or not.\n");
        
    //how to create a bitmask like 1000 0000
    /*
        Hex  	F  	E  	D  	C  	B  	A  	9  	8  	7  	6  	5  	4  	3  	2  	1
        Binary 	1111 	1110 	1101 	1100 	1011 	1010 	1001 	1000 	0111 	0110 	0101 	0100 	0011 	0010 	0001
        Dec 	15 	14 	13 	12 	11 	10 	9 	8 	7 	6 	5 	4 	3 	2 	1
    */
    
        while(i>0){
    	if(a&mask) putchar('1');
    	else	putchar('0');
    	i--;
    	a <<= 1;
        }
        printf("\n\n");
    
        return 0;
    }

  6. #6
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    thanks it worked.
    but isnt this only masking the first half?

  7. #7
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    i see.
    thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 08:29 AM
  2. function argument not needed
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2008, 05:34 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Replies: 4
    Last Post: 11-23-2003, 06:15 AM

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