Thread: Bitwise Operations are getting me mad.

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    Bitwise Operations are getting me mad.

    Hello,
    first say I'm not good with bit operations (I'm still learning and practing with them) so if my question is so trivial forgive me.

    I'm trying to modify a bit of a character and keep the others intact (the last one), when i try to put a one it seems be working but when i try to put a zero it works sometimes but others don't. The two simple functions I'm using are the following:
    Code:
    void put_one(char *c){
    *c|= 00000001;
    }
    
    void put_zero(char *c){
    *c&= 11111110;
    }
    For example if I use 't' (int value 116) as argument, put_one returns 'u' (int value 117) what seems to be correct. The problem is with put_zero, it should return the same value ('t' int value 116) but it returns 'D' int value 68.
    So, what I'm doing wrong? Is there any better way to do this task?

    Thanks in advance, and sry if this question is so basic.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Those numbers are not binary, they are decimal [well, technically 0000001 is an octal number, but it makes little difference].

    Try using decimal or hex form to describe your binary number.

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

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Assuming put_zero is trying to clear the least significant bit, the constant value you're and-ing with is wrong. The constant you've written is the decimal number 11111110, not the binary value. The decimal value you want to use is 254 = binary 11111110.

    In decimal, the low 8 bits of 11111110 = b'11000110. 116 = b'01110100. Anding the two together gives 01000100, which is the 68 you're seeing.

    Most people use hex (0xFE) or octal (0376) for this since there's a natural mapping between each digit and a fixed number of bits. For hex, each digit represents 4 bits (1111 = 0xf, 1110 = 0xe). For octal, each digit represents 3 bits (011 = 03, 0111 = 07, 0110 = 6).

    But for clearing bits, the typical C idiom is to use something like

    x &= ~1; // replace 1 with the value of the bits to clear

    The ~ operator flips all of the bits in the constant, so the value you put in indicates the bits you are clearing.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    Quote Originally Posted by matsp View Post
    Those numbers are not binary, they are decimal [well, technically 0000001 is an octal number, but it makes little difference].

    Try using decimal or hex form to describe your binary number.

    --
    Mats
    I really appreciate your help, I spent one day of my life without find any solution and you solved it in minutes!
    As you said using hex it worked
    Code:
    void put_zero(char *c){
    *c&= 0xfe;
    }
    
    void put_one(char *c){
    *c|=0x01;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Operations to Read ON/OFF Bits
    By pseudonoma in forum C Programming
    Replies: 4
    Last Post: 02-25-2008, 03:15 PM
  2. bitwise operations with double
    By henry_kay in forum C Programming
    Replies: 2
    Last Post: 10-03-2007, 04:57 AM
  3. Bitwise operations
    By sh3rpa in forum C++ Programming
    Replies: 16
    Last Post: 09-25-2007, 06:32 PM
  4. bitwise operations
    By black_watch in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 04:48 AM
  5. bitwise operations
    By andrew_tucker in forum C Programming
    Replies: 2
    Last Post: 11-28-2002, 12:46 AM