Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Set sixth bit to 1

This is a discussion on Set sixth bit to 1 within the C and C++ forums, part of the Software Development category; Given a sixteen bit number. How can sixth bit can be set to 1 irrespective of its initial value and sixth bit reset to zero irrespective of its initial value...

Go Back   Geeks Talk > Software Development > C and C++
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 11-16-2007
Junior Member
 
Join Date: Nov 2007
Location: india
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
dattu.kv is on a distinguished road
Set sixth bit to 1

Given a sixteen bit number. How can sixth bit can be set to 1 irrespective of its initial value and sixth bit reset to zero irrespective of its initial value
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-19-2007
Junior Member
 
Join Date: Nov 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
ruler_of_doll is on a distinguished road
Re: Set sixth bit to 1

suppose 'a' contains 16 bit number. And initialize b with 32.

a=a|b; /*this will convert 6th bit to One irrespective of its original bit*/

now for making 6th bit 0, load b with 65503 and do the following.
a=a&b;
Reply With Quote
  #3 (permalink)  
Old 12-04-2007
Junior Member
 
Join Date: Dec 2007
Location: India
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
nishant_naveen is on a distinguished road
Arrow Re: Set sixth bit to 1

for setting the 6th bit:
x | (1<<5)

for unsetting the 6th bit:
x & (~(1<<5))

hope it helps.
Reply With Quote
  #4 (permalink)  
Old 12-22-2007
Contributing Member
 
Join Date: Dec 2007
Posts: 48
Thanks: 1
Thanked 8 Times in 7 Posts
sk_seeker is on a distinguished road
Re: Set sixth bit to 1

Quote:
Originally Posted by dattu.kv View Post
Given a sixteen bit number. How can sixth bit can be set to 1 irrespective of its initial value and sixth bit reset to zero irrespective of its initial value
You could also use the OR and AND operators to help you put.

#define MASK1 0x0040 // Note that 6th bit is 1, counting from 0
#define MASK2 0xffbf // Note that the 6th bit is 0

// Set 6th bit of x to 1
x = x | MASK1

// Set 6th bit of x to 0
x = x & MASK2
Reply With Quote
  #5 (permalink)  
Old 01-10-2008
Junior Member
 
Join Date: Jan 2008
Location: india
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
sherleena is on a distinguished road
Re: Set sixth bit to 1

what the hell is the answr..
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -4. The time now is 05:29 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved