Sponsored Content
Full Discussion: toggle bit
Top Forums Programming toggle bit Post 302431605 by Praveen_218 on Tuesday 22nd of June 2010 09:06:44 AM
Old 06-22-2010
The direct answer to your question :

Code:
int main() {
unsigned short int v = 7;                             // First 13  MSB == 0; Last 3 LSB == 1 --> 0000000000000111
unsigned short int toggled_bit = (0xffff ^ v) ; // toggled_bit now have a bits pattern  --> 1111111111111000

   printf("Actual Variable == %u --- toggled bit value %u\n", v, toggled_bit);
}


The shortest answer, isn;t it?

Now a bigger code; write a function Smilie

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/errno.h>

unsigned short toggleBits ( unsigned short a_val) {
   return (0xffff ^ a_val);
};

int main(int argc, **argv) {
unsigned short int val = 0;
   if(argc < 2) exit(1);
   val = (unsigned short) atoi(argv[1]);
   printf(" val == %u\n" , val);
   for(int i = 0; i < 4; i++) {
      val = toggleBits(val);
      printf(" val == %u\n" , val);
   } 
   return 0;
}

However, if you wana just toggle a single bit (say n'th bit) , then instead of FFFF, use a value equivalent to 2^n (2 to the power n) and do the XOR operation exactly similar to the above code.

Let me know, if you need any specific question. You still need to explore yourself a lot by writing simple codes doing such bitwise operation to get more clarity.

Last edited by Praveen_218; 06-22-2010 at 02:05 PM.. Reason: Typo mistake corrections and some minor additions ...
This User Gave Thanks to Praveen_218 For This Post:
 

6 More Discussions You Might Find Interesting

1. Programming

copying or concatinating string from 1st bit, leaving 0th bit

Hello, If i have 2 strings str1 and str2, i would like to copy/concatenate str2 to str1, from 1st bit leaving the 0th bit. How do i do it? (2 Replies)
Discussion started by: jazz
2 Replies

2. Shell Programming and Scripting

Toggle Hidden Files Mac OS X

Hi all, I have been using Ubuntu for 2 years now, and a few days ago I bought a Macbook. This is my first time using a Mac, so I have spent the better of two days learning the user interface, and configuring my Macbook. One thing I noticed is that there is no easy way to turn on and off hidden... (0 Replies)
Discussion started by: Omniwheel
0 Replies

3. Shell Programming and Scripting

Perl script to toggle through dates by week

Hi, I need help to toggle through dates on a weekly basis to be fed into a script as inputs. The format should be: yyyy/mm/dd (start) yyyy/mm/dd (end), where end date is 7 days increments. The date (start) would be input as an ARGV and would continue until current date. I can check... (2 Replies)
Discussion started by: subhap
2 Replies

4. Shell Programming and Scripting

How to Toggle Flag/Switch Value with Sed

I am trying to figure out a one liner to toggle a flag variable. eg. FLAG=0 Is there a way to use sed to toggle above example between 0 and 1. That is if run with flag set to zero it would change it to one if run again it would set it to zero. I thought I had it figured but the... (6 Replies)
Discussion started by: bsquared
6 Replies

5. Shell Programming and Scripting

How to handle 64 bit arithmetic operation at 32 bit compiled perl interpreter?H

Hi, Here is the issue. From the program snippet I have Base: 0x1800000000, Size: 0x3FFE7FFFFFFFF which are of 40 and 56 bits. SO I used use bignum to do the math but summing them up I always failed having correct result. perl interpreter info, perl, v5.8.8 built for... (0 Replies)
Discussion started by: rrd1986
0 Replies

6. Windows & DOS: Issues & Discussions

Which version of Windows Vista to install with a product key? 32-bit or 64-bit?

Hello everyone. I bought a dell laptop (XPS M1330) online which came without a hard drive. There is a Windows Vista Ultimate OEMAct sticker with product key at the bottom case. I checked dell website (here) for this model and it says this model supports both 32 and 64-bit version of Windows... (4 Replies)
Discussion started by: milhan
4 Replies
DwtToggleButtonGadgetCreate(3Dwt)										 DwtToggleButtonGadgetCreate(3Dwt)

Name
       DwtToggleButtonGadgetCreate - Creates a toggle button gadget.

Syntax
       Widget DwtToggleButtonGadgetCreate (parent_widget, name,
					   override_arglist,
					   override_argcount)
	    Widget parent_widget;
	    char *name;
	    ArgList override_arglist;
	    int override_argcount;

Arguments
       parent_widget
		 Specifies the parent widget ID.

       name	 Specifies the name of the created widget.

       override_arglist
		 Specifies the application override argument list.

       override_argcount
		 Specifies the number of attributes in the application override argument list (override_arglist).

Description
       The  DwtToggleButtonGadgetCreate  function  creates an instance of the toggle button gadget and returns its associated gadget ID.  A toggle
       button gadget is similar in appearance and semantics to a toggle button widget.	Like all  gadgets,  DwtToggleButtonGadgetCreate  does  not
       have  a	window	but  uses the window of the closest antecedent widget.	Thus, the antecedent widget provides all event dispatching for the
       gadget.	This currently restricts gadgets to being descendents of menu or dialog class (or subclass) widgets.

Inherited Attributes
       ---------------------------------------------------------
       Attribute Name	       Data Type	Default
       ---------------------------------------------------------
       Rectangle Attributes
       DwtNx		       Position 	Determined   by
						the    geometry
						manager
       DwtNy		       Position 	Determined   by
						the    geometry
						manager
       DwtNwidth	       Dimension	The  width   of
						the  label plus
						margins
       DwtNheight	       Dimension	The  height  of
						the  label plus
						margins
       DwtNborderWidth	       Dimension	zero
       DwtNsensitive	       Boolean		True
       DwtNancestorSensitive   Boolean		The bitwise AND
						of  the  parent
						widget's
						DwtNsensitive
						and  DwtNances-
						torSensitive
						attributes

       Label Attributes

       DwtNlabel	       DwtCompString	Widget name

       DwtNalignment	       unsigned char	DwtAlignment-
						Center
       DwtNdirectionRToL       Boolean		False
       DwtNhelpCallback        DwtCallbackPtr	NULL
       ---------------------------------------------------------

Widget-Specific Attributes
       ------------------------------------------------------------
       Attribute Name		  Data Type	   Default
       ------------------------------------------------------------
       DwtNshape		  unsigned char    DwtRectangular
       DwtNvalue		  Boolean	   False
       DwtNvisibleWhenOff	  Boolean	   True
       DwtNvalueChangedCallback   DwtCallbackPtr   NULL
       DwtNbuttonAccelerator	  char *	   NULL
       DwtNacceleratorText	  DwtCompString    NULL
       ------------------------------------------------------------

       DwtNshape      Specifies the toggle button indicator shape.  You can pass DwtRectangular or DwtOval.

       DwtNvalue      Specifies a boolean value that, when False, indicates the button state is off.  If True, the button state is on.

       DwtNvisibleWhenOff
		      Specifies a boolean value that, when True, indicates that the toggle button is visible when in the off state.

       DwtNvalueChangedCallback
		      Specifies the callback function or functions called when the toggle button value was changed.  For this callback, the reason
		      is DwtCRValueChanged.

       DwtNbuttonAccelerator
		      Sets an accelerator on a toggle button widget.  This is the same as the DwtNtranslations core attribute except that only the
		      left  side  of  the  table  is to be passed as a character string, not compiled.	The application is responsible for calling
		      XtInstallAllAccelerators to install the accelerator where the application needs it.

       DwtNacceleratorText
		      Specifies the compound-string text displayed for the accelerator.

Return Values
       This function returns the ID of the created widget.

Callback Information
       The following structure is returned to your callback:
       typedef struct {
			       int reason;
			       XEvent *event;
			       int value;
       } DwtTogglebuttonCallbackStruct;

       The reason member is set to a constant that represents the reason why this callback was invoked.  For this callback, the reason member  can
       be set to:

       DwtCRValueChanged    The  user  activated  the
			    toggle button  to  change
			    state.

       DwtCRHelpRequested   The user selected Help.

       The  event  member  is a pointer to the Xlib structure XEvent, which describes the event that generated this callback.  This structure is a
       union of the individual structures declared for each event type.  For information on XEvent and event processing, see the Guide to the Xlib
       Library: C Language Binding.

       The value member is set to the toggle button's current state when the callback occurred, either True (on) or False (off).

See Also
       Guide to the XUI Toolkit: C Language Binding
       Guide to the XUI Toolkit Intrinsics: C Language Binding

														 DwtToggleButtonGadgetCreate(3Dwt)
All times are GMT -4. The time now is 01:25 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy