Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I was wondering if it is possible to index an array by using a binary number instead of a decimal number. For instance, arr[binary].
Yep that's definitely possible. Just prepend your binary number with 0b
int array[] = {1,2,4,6};
printf("%d\n", array[0b0001]); // prints 2
from https://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html binary constants can be written using the 0b syntax
As pointed out, all numbers stored on a computer are binary. Binary is the only thing that can be stored on a computer.
And, C does not support binary syntax. (Or perhaps come C compilers do?)
You could however convert a string from binary like this:
var value = arr[BinaryToInt("1011")];
int BinaryToInt(string s)
{
int value = 0;
int bitValue = 1;
for (int i = s.Length - 1; i >= 0; i--)
{
if (s[i] == '1')
value += bitValue;
bitValue <<= 1;
}
return value;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I just wonder how to find mean of two numbers without using division.
do not use these conditions :
int mean = (a + b) >> 1;
four fundamental arithmetic operations
I think this may be helpful -->
int a,b,i,j;
if (a>b)
{
int temp = a;
a = b;
b = temp;
}
for(i=a,j=b;i<j;i++,j--)
continue;
if(i==j)printf("%d\n", i);
else printf("%lf\n", (double)(i)-0.5);
Add them then multiply by 0.5 , no division involved.
If they're both integers, you can use a right shift:
int median = (a + b) >> 1;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The program is suppose to convert binary number to decimal form. Only using scanf() and printf() library functions. Takes in a char array from user ---no prompt outputs decimal form, function must be used with parameter (char binaryString[]) after conversion result must be printed out in main. Program does not work don't think I'm converting the binary form to decimal form correctly in function binaryToDecimal since i cant use pow() I'm lost
#include <stdio.h>
#include <math.h>
int binaryToDecimal(char binaryString[]) {
int c, j = 1, decimalNumber = 0;
for (binaryString[c = 0]; binaryString[c] > binaryString[33];
binaryString[++c]) {
while (binaryString[c] != 0) {
remainder = binaryString[c] % 10;
decimalNumber = decimalNumber + remainder * j;
j = j * 2;
binaryString[c] = binaryString[c] / 10;
}
}
return decimalNumber;
}
int binaryToDecimalMain() {
int arraysize = 33;
char binaryString[arraysize];
scanf("%32s", binaryString);
printf("%d",binaryToDecimal(binaryString []);
return 0;
}
I not give you the algorithm because it's seems that you are learning how to program and it is important to you to learn to discover how to solve the problems that are given to you.But I can give you some hints:
use binaryString only to compare with '0' or '1'. Don't try to make any operations like '%' on it.
iterate on the binaryString character by character (no while inside for [this is only for this case, there some algorithm that is necessary to do something like this])
your logic to convert is on the right track
Also you should call your main function main.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
What would be the most efficient func to convert a binary to 12 character, base 36, left zero-padded? At the moment I have come up with this:
int transform_id (int64_t id_value, char* str) {
char num[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ??";
int i;
for (i = 11; i >= 0; id_value /= 36) {
str [i--] = num [id_value % 36];
}
str [12] = 0;
return (12);
}
... which seems to work but i'm not sure if there is a far superior method O(n) wise
well, table lookup is the fastest method I think. The problem is base because it's not, well, "bit-aligned". if your base was 2^n, then you could use bitwise shifts and ANDs to get indices, and now you have to use division and deal with remainder
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to know if there is any function in the C language which can convert base of decimal number binary or Decimal to Hexadecimal or etc.
You can try this..
int convert(int number,int base){
if(number == 0 || base==10)
return number;
return (number % base) + 10*convert(number / base, base);
}
int main () {
int a;
scanf("%d",&a);
//Lets convert 123 in decimal to octal
printf("%d\n",convert(123, 8));
return 0;
}
Let me know if this helps.. :)
There is no standard libraries or functions to do that
You can always write a small function to do your calculations.. It won't be hard to find codes (If you don't wanna write) but I suggest to write them your self with your basic knowledge on number conversions..
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I wanted to find out efficiently which drawers are Full. But the output has to be a number corresponding to the binary representation. For example if only the second Drawer is full (from left to right), then the output is 4:
8 4 2 1
0 1 0 0 (Drawer Two is Full)
So I used this approach.
int DrawersFull[4] = {0,0,0,0}; //Initially all are empty
for(i=0;i<4;i++)
{
if(IsDrawerFull) // the api was provided by the interviewer
DrawerFull[i]=1;
}
I am not sure how to generate the output. Any suggestions will be helpful. Thanks.
Interviewer gave me hint that it can be done using bitwise operators but I am not sure.
This is the same as converting binary number to decimal. Try this one:
int res = 0;
for (i = 4; i >= 1; i--) {
res = res * 2 + DrawerFull[i]; // Assuming DrawerFull will contain only 1 or 0.
}
char fullDrawers = 0;
for( char i = 0; i < 4; ++i )
{
if( IsDrawerFull )
fullDrawers &= 1;
fullDrawers <<= 1;
}