본문 바로가기

Programming

How to use bitset, bitwise operator in c++, 비트셋, 비트연산자 사용하는 방법, 이진수 변환 방법, How to convert decimal to binary, binary to decimal

How to use bitset, bitwise operator in c++, 비트셋, 비트연산자 사용하는 방법, 이진수 변환 방법, How to convert decimal to binary, binary to decimal


Java ( Here ) not yet



bitset 초기화 방법 - 예제코드 확인

 -> bitset<원하는 bit 사이즈> testBit(십진수)


Initialize bitset - check sample code

 -> bitset<bit size you want> testBit(decimal number)



bitset 에서 정수로 변환

 -> (int)(testBit.to_ulong())

bitset to integer

 -> (int)(testBit.to_ulong())





bitset 그대로 출력할 경우, 정해둔 bit 사이즈의 이진수로 출력된다.

이진수의 맨 앞 1 앞부분은 0으로 채워진다. e.g. 00001010 (in 8 bit size)

정해둔 bit 사이즈를 초과하는 정수를 넣을 경우는, 초과한 만큼 이진수의 앞부분이 짤려서 출력된다.



If you just cout bitset, then we can see binary with bit size you set.

Zeros will be filled in front of first 1 in binary. e.g. 00001010 (in 8 bit size)

If decimal number is bigger than setting bit size, then front binary number will be deleted





#include <iostream>

#include <stdio.h>

#include <bitset>



using namespace std;



int main(int argc, char *argv[]) {


bitset<2> testBit2(7);


cout << testBit2 << endl;

cout << (int)(testBit2.to_ulong()) << endl << endl;


bitset<8> testBit8(100);


cout << testBit8 << endl;

cout << (int)(testBit8.to_ulong()) << endl << endl;

bitset<16> testBit16(100);


cout << testBit16 << endl;

cout << (int)(testBit16.to_ulong()) << endl << endl;


bitset<32> testBit32(100);


cout << testBit32 << endl;

cout << (int)(testBit32.to_ulong()) << endl << endl;


bitset<64> testBit64(100);


cout << testBit64 << endl;

cout << (int)(testBit64.to_ulong()) << endl << endl;

}




Something else you might like...?




2019/02/17 - [Programming/C++] - How to add/insert char in front of string in c++, 스트링 문자 앞에 char문자 캐릭터 추가하는 방법, 예제, example code, java

2019/02/12 - [Programming/C++] - How to use map in c++ STL, c++ 맵 STL 사용방법/초기화/예제, java, example, sample, library, find, insert, first

2019/02/04 - [Programming/C++] - How to add DLL / external lib file to VisualStudio, 비주얼스튜디오 외부라이브러리 dll 파일 추가 방법, example, c++ java api


2019/01/31 - [Programming/Java] - How to convert Decimal to Hexa, 10진수 숫자 16진수 숫자로 변환/바꾸기, Java c++, example, code



2019/01/31 - [Algorithm/Leet Code] - LeetCode #593 ValidSquare. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/02/03 - [Algorithm/Leet Code] - LeetCode #398 RandomPickIndex. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/02/09 - [Algorithm/Leet Code] - LeetCode #709 ToLowerCase. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/02/18 - [Algorithm/Leet Code] - LeetCode #412 FizzBuzz. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/02/24 - [Algorithm/Leet Code] - LeetCode #476 NumberComplement. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/02/13 - [Algorithm/Leet Code] - LeetCode #258 AddDigits. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접



2019/02/16 - [Life/Health care] - Finasteride 피나스테라이드,탈모약 usage/side effects/dosage 효능/부작용/효과/sexual effect/두타스테라이드/프로페시아/propecia/finpecia/카피약/copy drug/hair loss

2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/01/25 - [Life/Health care] - L-Arginine 아르기닌 usage/side effects/dosage 효능/부작용/성인,소아 용법(3)

2019/01/25 - [Life/Health care] - Maca 마카 usage/side effects/dosage 효능/부작용/성인,소아 용법(2)

2019/01/28 - [Life/Health care] - Rogaine 로게인(Minoxidil, 미녹시딜, 마이녹실) usage/sideEffects/dosage 효능/부작용/사용법