[英]How do I split my binary results into groups of four with C. The aim of the code is convert and decimal figure into binary notation
例如, 233的二進制轉換是11101001,並且想要將其轉換為1110 | 1001之類的東西
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, r, decimal, binary[20];
while(1)
{
printf("\n\nPLEASE ENTER ANY DECIMAL NUMBER: ");
scanf("%d", &decimal);
int n = decimal;
i = 0;
while (decimal > 0)
{
r = decimal % 2;
binary[i] = r;
decimal = decimal / 2;
i++;
}
printf("\n BINARY EQUIVALENT OF %d is; ", n);
for (j = i - 1; j >= 0; j--)
{
printf("%d", binary[j]);
}
}
return 0;
}
我敢打賭,對於某些人來說,這是一個菜鳥問題,但我可以使用您的幫助,謝謝
有趣的是,我昨天剛剛這樣做。 這可能不是執行此操作的最佳方法,但它可以工作。
char* hex_to_bin (unsigned char c) {
static char bin[10];
strcpy (bin, "");
char* mappings[] = {
"0000",
"0001",
"0010",
"0011",
"0100",
"0101",
"0110",
"0111",
"1000",
"1001",
"1010",
"1011",
"1100",
"1101",
"1110",
"1111"
};
sprintf (bin, "%s %s", mappings [(c/16) % 16], mappings[c % 16]);
return bin;
}
void print_bin (void * ptr, size_t size) {
int i;
for (i = size - 1; i >= 0 ; i--) {
unsigned char c = ((char*)ptr)[i];
printf ("%s ", hex_to_bin(c));
}
}
使用方法:
int k = 431;
print_bin (&k, sizeof (int)); // Of course you can put any pointer here
它應該打印此:
0000 0000 0000 0000 0000 0001 1010 1111
注意,在for
我從size
迭代到0。這特定於Little Endian處理器。 如果您有Big Endian處理器,則必須從0
擴展到size
例如,您可以在“ j”的第二個外觀中添加條件,並在打印4位數字后添加|。
for (int j=i-1; j>=0; j--) {
printf( "%d",binary[j]);
if(j%4==0 && j!=0)
printf("|");
}
如果只想打印它,建議使用按位操作數和位移 。 對C和按位操作數中的移位(如果您當然還不知道)進行一些研究,然后查看下面的代碼和說明。 這是一個更簡化的代碼,並且性能也更高,並且您將通過變量聲明節省內存。
很少解釋: 按位操作數比較一個或兩個變量的每個單個位。 通過位移位,可以將變量的位向所需次數的方向移位 。
參見下面的代碼:
#include <stdio.h>
void main() {
int i, decimal;
//The mask to compare just one bit
int mask=0b10000000;
printf("\n\nPLEASE ENTER ANY DECIMAL NUMBER: ");
scanf("%d", &decimal);
//Loopping 8 bits only
for(i=0;i<8;i++) {
/**
* Here you are doing the "magic", see how:
* You're using AND bitwise operand to compare just one bit at time starting for the MSB as you can see in first mask value
* So, first comparison with AND is you're value, for example 254, with mask shifted of 'i' bits, i value is 0 here
* The first comparison so is: (255)11111110 -> YOUR INT VALUE
* (AND)& 10000000 -> MASK
* Result: 10000000 -> THE RESULT OF: decimal&mask
* But this result in decimal is 128 and you want to show just 1 or 0, being 1 for everything that is not and 0 and 0 only for 0 results
* Here I suggest a technique, use the NOT logical operand to invert the value logically and the do it again, as bellow:
* !10000000 result in 0 and !0 result in 1 or 00000001, this way you can show if the bit is HIGH or DOWN.
* Keep doing it for the other bits, let see more 2:
* -----------------------------------------------------------------------------------------------
* Now i = 1 (second time in for loop), so mask is shifted 1 bit to right and mask is 01000000
* Making the same comparison (255)11111110 -> YOUR INT VALUE
* (AND)& 01000000 -> MASK
* Result: 01000000 -> THE RESULT OF: decimal&mask
* When you invert the result twice the result is 1 again.
* ------------------------------------------------------------------------------------------------
* Now i = 7 (last (seventh) time in for loop), so mask is shifted 7 bits to right and mask is 00000001
* Making the same comparison (255)11111110 -> YOUR INT VALUE
* (AND)& 00000001 -> MASK
* Result: 00000000 -> THE RESULT OF: decimal&mask
* When you invert the result twice the result is now 0. Because 0&1 is 0, when inverted it is 1 and inverting again it turns 0.
*
*/
printf("%d", !(!(decimal&mask>>i)));
if(i==3) printf(" "); //If it is on the fourth bit will print a space
}
}
您可以使用掩碼,按位運算符可將<<和AND&移位。 如果逐步調試器中的代碼,您將看到它是如何工作的。
void displayBits(unsigned);
int main(int args, char *argsv[]){
unsigned x;
printf("Enter an unsigned integer %u\n", x );
scanf("%u",&x);
displayBits(x);
return 0;
}
void displayBits(unsigned value){
const int SHIFT = 8*sizeof(unsigned)-1;
const unsigned MASK = 1 << SHIFT;
printf("%u = ", value);
unsigned c;
for(c=1; c<=SHIFT + 1; c++){
if(value & MASK){
printf("%d", 1);
}
else
{
printf("%d",0);
}
if(c%8==0)
printf("|");
value <<=1;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.