Problem in bitwise

This is a discussion on Problem in bitwise within the C Programming forums, part of the General Programming Boards category; i rlly dunt understand how to turn bit off but as book says to turn bit on you do #define ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    Problem in bitwise

    i rlly dunt understand how to turn bit off but as book says to turn bit on you do
    #define MARRIED (1<<3) //whish is = 8
    and status =status | married; <<how the hell it turn bit on
    lets say status == 5
    and 8(00001000)
    5(00000101)
    you do or it will just change
    to this (00001101);
    how it suppose to turn it on ??
    i understand that turn off bit
    status |=~MARRIED; i understand that
    and if(Status & married);

    can please also someone show the uses of flags coz this chapter is rlly anoying thanks alot guys

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    bitwise or says that bit n will be set if bit n in either of the operands is set. take 5 and 8.
    Code:
    00001000
    00000101 |
    --------
    00001101
    oring them together gives you 00001101 because those three bits were set in either 5 or 8. turning a bit off works the same way. bitwise and says that bit n will be set if bit n in both of the operands is set. take 5 and 4
    Code:
    00000100
    00000101 &
    --------
    00000100
    the first bit is turned off because there's only one set and not two. but what you wanted to do is turn off the third bit. so you invert 4 to make it a mask. when you make a mask it means that all but the bit you want is set. that way when the and happens, the bit you want gets unset.
    Code:
    11111011
    00000101 &
    --------
    00000001
    That's why setting a bit uses or and unsetting a bit uses inversion and and.
    Code:
    #define BIT(i) (1<<i)
    #define SET(x,i) ((x) | BIT(i))
    #define UNSET(x,i) ((x) & ~BIT(i))

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    oh thanks i get it now

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    To set a bit:

    Code:
    val |= BIT;
    To unset a bit:

    Code:
    val &= ~BIT;
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'm a bit late with this (it took some time) but I think it demonstrates all of the bit operations in an easy to understand way:
    Code:
    #include <string.h>
    #include <stdio.h>
    
    typedef struct {
    	unsigned char b7:1;
    	unsigned char b6:1;
    	unsigned char b5:1;
    	unsigned char b4:1;
    	unsigned char b3:1;
    	unsigned char b2:1;
    	unsigned char b1:1;
    	unsigned char b0:1;
    } _byte;
    
    union byte {
    	unsigned char X;
    	_byte B;
    };
    
    char string[9];
    
    char *byte2str (union byte A) {
    	sprintf(string, "%d-%d-%d-%d-%d-%d-%d-%d",A.B.b0,A.B.b1,A.B.b2,A.B.b3,A.B.b4,A.B.b5,A.B.b6,A.B.b7);
    	return string;
    }
    
    int main(int argc, char *argv[]) {
    	union byte input, up, down;
    	unsigned char I=atoi(argv[1]);
    	
    	input.X=I;
            up.X=I<<atoi(argv[2]); 
    	down.X=I>>atoi(argv[2]);
    	
    	printf("input=%d \t%s\n",input,byte2str(input));
    	input.X=~input.X;
    	printf("  not=%d \t%s\n",input,byte2str(input));
    	input.X=I; input.X&=up.X;
    	printf("\n    up=%d \t%s\n",up,byte2str(up));
    	printf("and up=%d \t%s\n",input,byte2str(input));
    	input.X=I; input.X|=down.X;
    	printf("\n   down=%d \t%s\n",down,byte2str(down));
    	printf("or down=%d \t%s\n",input,byte2str(input));
    	return 0;
    }
    You give it two numbers, the first is "input" and the second the size of a shift:
    Code:
    ./a.out 5 2
    input=5 	0-0-0-0-0-1-0-1
      not=250 	1-1-1-1-1-0-1-0
    
        up=20 	0-0-0-1-0-1-0-0
    and up=4 	0-0-0-0-0-1-0-0
    
       down=1 	0-0-0-0-0-0-0-1
    or down=5 	0-0-0-0-0-1-0-1
    "or down" is the same as input since input already has the least significant bit set.
    Code:
    ./a.out 57 3
    input=57 	0-0-1-1-1-0-0-1
      not=198 	1-1-0-0-0-1-1-0
    
        up=200 	1-1-0-0-1-0-0-0
    and up=8 	0-0-0-0-1-0-0-0
    
       down=7 	0-0-0-0-0-1-1-1
    or down=63 	0-0-1-1-1-1-1-1
    ps. this is nifty if you change I and X to signed but then you have to "legalize" some stuff in the printf statements.
    Last edited by MK27; 02-05-2009 at 02:38 PM.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yah thanks alot i understand it now i also did this code for flags
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define MALE (1<<0)
    struct employee
    {
           int flags;
    };typedef struct employee emp;
    void read_in(emp * e)
    {
      char answer;
      e->flags=0;
      fputs("Female or male (F/M) ? ",stdout);
      answer=getchar();
      if(toupper(answer)=='M')
              e->flags|=MALE;
      else
              e->flags&=~MALE;
    }
    void print_emp(emp * e)
    {
         puts( (e->flags &&  MALE )? "MALE" : "FEMALE");
         getchar();
    }
    int main(void)
    {
        emp e;
        read_in(&e);
        print_emp(&e);
        return getchar();
    }

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    A little clearer and simpler:
    Code:
    #define MALE 1
    puts( (e->flags)? "MALE" : "FEMALE");
    There is no point to (e->flags && MALE) -- perhaps you meant (e->flags==MALE).
    I wanted to point that out because you are not really using a bit flag here (whether it's == or &&), you are just testing whether e->flags is 0 (false) or more than 0 (true) which it would be if you bit shifted it.

    Or perhaps you really meant &=
    Last edited by MK27; 02-05-2009 at 04:48 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    Quote Originally Posted by MK27 View Post
    A little clearer and simpler:
    Code:
    #define MALE 1
    puts( (e->flags)? "MALE" : "FEMALE");
    There is no point to (e->flags && MALE) -- perhaps you meant (e->flags==MALE).
    I wanted to point that out because you are not really using a bit flag here (whether it's == or &&), you are just testing whether e->flags is 0 (false) or more than 0 (true) which it would be if you bit shifted it.

    Or perhaps you really meant &=
    no that chk if its turned on or off if there on then its male if its off then its female

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    A little clearer and simpler:
    Code:
    #define MALE 1
    puts( (e->flags)? "MALE" : "FEMALE");
    There is no point to (e->flags && MALE) -- perhaps you meant (e->flags==MALE).
    I wanted to point that out because you are not really using a bit flag here (whether it's == or &&), you are just testing whether e->flags is 0 (false) or more than 0 (true) which it would be if you bit shifted it.

    Or perhaps you really meant &=
    I think the assumption is that there will, eventually, be other flags than just MALE -- and so checking which flag is actually set is then important. (Edit: Granted it should be &.)
    Last edited by tabstop; 02-05-2009 at 05:33 PM.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lolguy View Post
    no that chk if its turned on or off if there on then its male if its off then its female
    I think the short story is that you have tricked yourself into believing your theory is correct because you got a correct answer. BUT, if e->flags were 4, (in which case the first bit is not set), your function would still return true because 4 is greater than zero.
    So using e->flags in this way, you cannot usefully add, for example:
    Code:
    #define EMPLOYED 2<<0
    Because your function is not checking the bits of e->flags, it's just checking to see if it's more than zero. && MALE is meaningless because it now means if ((e->flags>0) && (MALE>0)). Of course male is greater than zero.

    You could use:
    Code:
    puts( (e->flags &&  sky==blue)? "MALE" : "FEMALE");
    But what's the point?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    e->flags is a int not a bit u cant compare it u must first to change it to bits by comparing if turned on before
    [code]
    if(toupper(answer)=='M')
    e->flags|=MALE;
    else
    e->flags&=~MALE;
    here check if its on or off if he puts M or m it bit will be on

    else it will be off also for your code the = has higher precedence than the & so it wont work.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    1
    Hi brewbuck,

    May i ask for the code,

    val |= BIT;

    how does the "|=" operator work?

    I understand how they work individually, but don't understand how they are used together.

    Cheers

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    lolguy: I am just warning you, if it matters YOU ARE WRONG. I in no way implied that e->flags was anything but an int. You need to believe me, do a couple of experiments, and think harder.

    panmingen google "bitshift C tutorial" and "bitwise operations C tutorial", read whatever you want, then look at post #5.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just to clarify this snippet:
    Code:
    #define EMPLOYED 2<<0
    Generally, we'd use 1 << X, where X is the bit number, e.g:
    Code:
    #define EMPLOYED 1 << 0
    #define MARRIED    1 << 1
    #define MALE          1 << 2
    Another helpful trick is to use enum instead of #define:
    Code:
    enum FlagBits
    {
         Employed = 1 << 0,
         Married = 1 << 1,
         Male = 1 << 2
    };
    That way, a debugger MAY be able to show you at least the value of individual bits, even if it can't figure out that (6) means (Married | Male).

    --
    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.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    Generally, we'd use 1 << X, where X is the bit number,
    Good idea
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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