Bit operations on different datatypes

This is a discussion on Bit operations on different datatypes within the C Programming forums, part of the General Programming Boards category; Hello, I've written som code that parses through an array of chars and performs bit operations on each char ( ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Bit operations on different datatypes

    Hello,

    I've written som code that parses through an array of chars and performs bit operations on each char ( each byte).

    Now what i would like to do is to reuse this code to parse through all of the bytes of an array of floats and perform those very same bit operations on each byte.

    How to best go about this?

    If i have my float array:

    float* float_array = (float*)malloc(...);

    can i then just do:

    unsigned char* char_array = (unsigned char*)(float_array);

    And just reuse my old code?

    Any advice would be much appreciated! Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    What bit operations are you planning on an array of floats, that makes any sense at all?
    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.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Hi Salem,

    Well just for fun I've written a small bit compression algorithm (similar to Huffman coding) that identifies identical bytes or series of bytes and basically compresses these to a smaller size.

    Now i believe this should be very possible to do on float arrays aswell on a byte by byte level. Does that make any sense to you?

    Thank you for your reply.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Sure, you can do things like that.
    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.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Salem View Post
    What bit operations are you planning on an array of floats, that makes any sense at all?
    There's a cool trick you can do to take square roots really fast using IEEE floats, that depends on their bit representation.

    It was used in Doom and I've used it in software as well.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Yes i believe that was John Carmack ( spelling? ) for reciprocal square root in Quake. However since 1999 x86 has faster built in functions (ref: Some Assembly Required Blog Archive Timing square root).

    I've seen the code segment, there was a comment like // what the **ck?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DWARF-2 expression error
    By programhelp in forum C++ Programming
    Replies: 3
    Last Post: 08-26-2010, 06:18 PM
  2. Bit operations?
    By yu_raider in forum C Programming
    Replies: 5
    Last Post: 03-17-2009, 11:31 AM
  3. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 06:59 AM
  4. Replies: 7
    Last Post: 12-10-2004, 07:18 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

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