#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Representin' Quebec
    Posts
    106
    Rep Power
    11

    Question Manipulating Bits


    Ok, maybe this is obvious but I have no idea what these do:
    manipulating bits with '~', '&', '|' and '^'. There was an example program in my book to show us what they do. But I don't get it at all!!! And also the bit shifters ( >> and <<) why would we need those???
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Gloucester, Glos
    Posts
    9
    Rep Power
    0
    hi gangsta
    The operators are for performing the bitwise operations in the logical op's

    if(a=='y')||(a=='Y')

    test all the bits eg a ='y' OR 'Y'

    in bitwise we are testing all bits in binary form we can test if all the bits are set to one

    AND
    01101111
    11111111
    & 01101111= FALSE

    we can isolate a lower nibble or byte etc
    mask = 00001111

    if we apply this to any byte all the upper nibble will be ignored
    as the 4 zeros automatically set this to 0000

    The | OR operator is the same however the test is for either bit being 1

    The ~ negates a byte etc
    ~11110000= 00001111.
    The bit shifters are used for either left or right shifting a byte
    multiplication / division can be achieved by shifting.
    All these operators are useful for low level work where you are interested in the bits not the whole variable say you had a value input through say a shift register and bit 3 when on signalled a switch had been activated on say a control board somewhere you would only be interested in this bit not the others & useful?
    ^ Xor I believe Xor a number with itself zero's all bits - tests for equality.

    Hope this helps
    Dave
  4. #3
  5. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    numbers on a computer are made up of bits (0's and 1's). all those operators what you mentioned are to binary numbers what + - * / are to usual numbers. not literally - they do different things but that's how they fit in with binary numbers. they're the basic operators for binary digits. so they allow you to work with and manipulate 0's and 1's, just like + and * do things to usual numbers.

    howstuffworks.com have got a few great pieces on this sort of thing. have a look at bits and bytes, and boolean logic:

    bits and bytes (should look at first) : http://computer.howstuffworks.com/bytes.htm
    boolean logic (& ~ ^ etc. here) : http://computer.howstuffworks.com/boolean.htm

    oh yeah, i don't think this is mentioned on that hsw boolean page:

    ~ = NOT
    & = AND
    | = OR
    ^ = XOR
    << = shift left
    >> = shift right



    2 things that further complicate the issue:

    you can't feed in 0's and 1's in c source as is - there's no direct provision for binary numbers. you have to give it your 0's and 1's in another form (eg 5 is the decimal version of 101)

    you have to do a bit of extra work in order to, say, & one bit with another because c's smallest data type is char (don't be put off by char equating with text - it's just a number). so there's not really a 1bit data type in c. you more often than not do group/block operations, and if you really did just want to manipulate 1bit of the 8bits you're operating on, you mask the other 7 off appropriately.


    this might be handy to have while you're looking at this sort of thing. it's a decimal to binary converter. type in say 99 and it gives 1100011 which is 99 in binary :
    Code:
    #include <stdio.h>
    
    /* i2b: print a in it's binary form */
    void i2b(int a)
    {
    	int b=64;
    	char c[65];
    	c[b]=0;
    	for(;a;a/=2)
    		c[--b]=a&1?'1':'0';
    	puts(c+b);
    }
    
    int
    main()
    {
    	int i=0;
    	while(1){
    		printf(":");
    		scanf("%u", &i);
    		i2b(i);
    	}
    }
    Last edited by balance; July 5th, 2003 at 03:54 PM.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Gloucester, Glos
    Posts
    9
    Rep Power
    0
    Hello Balance
    I was only using the binary format to show the theory behind the fact that the all numbers on a computer are constructed of '0' and '1' If you need to express a number in an AND or OR format you would under 'C' use a char etc as an 8 bit number but the theory is just the same AND under any number produces the same results you can't access the bits as binary but this is what is going on underneath in assembler.
    Dave
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Representin' Quebec
    Posts
    106
    Rep Power
    11

    Smile


    Thanks a lot both of you!!! Now I get it perfectly. :cool:

IMN logo majestic logo threadwatch logo seochat tools logo