Thread: Macro

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    India
    Posts
    3

    Macro

    Using which macro, MSB of a no. can be set??

  2. #2
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    if you consider a 32 bit int you can use
    Code:
    int i = 0;
    i = ( i | ( 1 << 32 )  ); // Set MSB

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Of course, doing this without assuming a specific number of bits in the number is more interesting.
    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.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pankaj401 View Post
    if you consider a 32 bit int you can use
    Code:
    int i = 0;
    i = ( i | ( 1 << 32 )  ); // Set MSB
    Sureley that should be
    Code:
    int i = 0;
    i = ( i | ( 1 << 31 )  ); // Set MSB
    ??

    There is no macro [as far as I know] that sets the MSB of an arbitrary integer, but you can do something like this:
    Code:
    #define SETMSB(x) (x) |= 1 << (sizeof(x) * CHAR_BITS))
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    Code:
    #define SETMSB(x) (x) |= 1 << (sizeof(x) * CHAR_BITS))
    Shouldn't it be?
    Code:
    #define SETMSB(x) (x) |= 1 << ((sizeof(x) * CHAR_BITS) - 1))
    My guess is that all this would only work for datatypes that can fit into processor registers.
    Last edited by pankaj401; 01-10-2008 at 05:19 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pankaj401 View Post
    Shouldn't be
    Code:
    #define SETMSB(x) (x) |= 1 << ((sizeof(x) * CHAR_BITS) - 1))
    My guess is that all this would only work for datatypes that can fit into processor registers.
    Of course it should - I pointed out your mistake, then made the same one myself.

    It would work for any type that the compiler is capable of producing a correct shift left operation for, so for example a _int64 type on MS Visual Studio, or "long long" in gcc, both work on 32-bit targets. The code produced will perform the correct multiinstruction shift, using for the x86 instruction SHLD for example. It is entirely possible to produce code on machines that doesn't have a specific SHLD instruction too - by using rotate with carry for example.

    Although I guess if you use "long long", the 1 would need to be postfixed by "LL" to make sure that the compiler doesn't say to itself "1 is an 32-bit integer, shift that more than 31 bits and it turns into zero, so let's remove all the code here". This can be fixed, either by multiple macros [for different types], or by passing the type of the data in and using that to cast the 1 to the correct type.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Macro Program
    By daspope in forum Windows Programming
    Replies: 5
    Last Post: 04-25-2004, 04:02 AM
  5. about Makefile and Macro
    By tom_mk in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 01:07 PM