"C" to write a function to place a number in the specified position 0 or 1

Source: Internet
Author: User
Write a function to place a number at a specified position of 0 or 1#include <stdio.h>unsigned int set_bit (unsigned int num, int pos, int flag) {int n = 1;n = n < ;< (pos-1);    The POS position of n is 1, the other is all 0if (flag = = 0) {num = num& (~n);} else if (flag = = 1) {num = num | n;} elseprintf ("Flag error\n"); return num;} int main () {printf ("%d\n", Set_bit (3,1,0));     2printf ("%d\n", Set_bit (4, 2, 1));   6printf ("%d\n", Set_bit (5, 1));  31return 0;} 

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"C" to write a function to place a number in the specified position 0 or 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.