CHAR_BIT in C
CHAR_BIT : It is the number of bits in char. These days, almost all architectures use 8 bits per byte (But it is not the case always, some older machines used to have 7-bit byte). It can be found in
Let us see an application of it. Suppose we wish to print byte by byte representation of an integer.
Examples :
Input : 4 Output : 00000000 00000000 00000000 00000100 Input : 12 Output : 00000000 00000000 00000000 00001100
// CPP program to print byte by byte presentation #include <bits/stdc++.h> using namespace std; // function in which number and intitally 0 is passed void printInBinary(int num) { int n = CHAR_BIT*sizeof(num); stack<bool> s; for (int i=1; i<=n; i++) { s.push(num%2); num = num/2; } for (int i=1; i<=n; i++) { cout << s.top(); s.pop(); // Put a space after every byte. if (i % CHAR_BIT == 0) cout << " "; } } int main() { int num = 12; printInBinary(num); return 0; } |
chevron_right
filter_none
Output :
00000000 00000000 00000000 00001100
This article is contributed by Apurva Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Check if the XOR of an array of integers is Even or Odd
- Check if original Array is retained after performing XOR with M exactly K times
- Count number of binary strings without consecutive 1’s : Set 2
- Different ways to use Const with Reference to a Pointer in C++
- std::to_address in C++ with Examples
- Count of even and odd set bit with array element after XOR with K
- Count of even set bits between XOR of two arrays
- Program to create Custom Vector Class in C++
- std::is_trivially_copy_constructible in C/C++
- Difference between Python and C++
- tgamma() method in C/C++ with Examples
- boost::type_traits::is_array Template in C++
- boost is_pointer template in C++
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits

