Bit Usage

This is a discussion on Bit Usage within the C Programming forums, part of the General Programming Boards category; You can add/subtract a mask... For example: Code: #include <iostream> int ToggleBit(int number, int bit_num) { int mask = 1; ...

  1. #16
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    You can add/subtract a mask...

    For example:

    Code:
    #include <iostream>
    
    int ToggleBit(int number, int bit_num)
    {
    	
    	int mask = 1;
    	
    	for(int loop = 0; loop < bit_num; loop++)
    		mask *= 2;
    	
    	if((number & mask) != 0)
    		number -= mask;
    	
    	else number += mask;
    	
    	return number;
    	
    }
    
    int main()
    {
    	
    	std::cout << std::hex;
    	std::cout << ToggleBit(0x20, 3);
    	
    	std::cin.get();
    	return 0;
    	
    }
    And my output is

    28

    It toggled the 3rd bit.
    What is C++?

  2. #17
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I put together a little function which may help you understand it better.

    Code:
    /*
     * This function will set or clear a particular bit.
     * Parameters:
     *		bit - the bit to change.  1 is least significant.
     *		someInt - pointer to integer to operate on.
     *		set - tells whether to set or clear the bit
     */
    void ChangeBitValue(int bit, int *someInt, int set)
    {
    	int mask = 1;
    
    	mask = mask << (bit - 1);
    
    	if(set)
    		*someInt = *someInt | mask;
    	else
    		*someInt = *someInt & ~mask;
    }

  3. #18
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    lol.

    As, you can see, there are many different ways to go about bit manipulation.
    What is C++?

  4. #19
    Wannabe Geek
    Join Date
    Aug 2004
    Posts
    19
    Thanx.
    Is there any way i can get the system to store an integer in 3 bytes.
    And how do i do consecutive reads on a two dimensional array.
    eg:- If i had an array[8][9] how can i read it 6 values at a time. That is i want to read the whole array, by reading 6 values of it at a time.
    -Cheers.

  5. #20
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Is there any way i can get the system to store an integer in 3 bytes.
    I'm sure you could do it manually with a union or an array of 3 chars (with bitwise operations, of course).

    And how do i do consecutive reads on a two dimensional array.
    It's all in how you manage the destination array and the loop that processes it. That's all I can tell you short of doing the problem for you.

    And no I'm not going to do the problem for you.

  6. #21
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there any way i can get the system to store an integer in 3 bytes.
    Yes, but you would still probably be using at least 4 bytes of storage and why would you want to do this anyway?

    >That is i want to read the whole array, by reading 6 values of it at a time.
    Hmm, something like this perhaps?
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int a[12];
      int i = 0;
      int n = 0;
    
      while ( i < 12 ) {
        /* Eew */
        i += n = scanf ( "%d %d %d %d %d %d", &a[i], &a[i + 1], &a[i + 2], &a[i + 3], &a[i + 4], &a[i + 5] );
    
        if ( n != 6 )
          break;
      }
    
      while ( --i >= 0 )
        printf ( "%d\n", a[i] );
    
      return 0;
    }
    My best code is written with the delete key.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-10-2005, 09:53 AM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  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