decimal number convert to 16bit binary (C)
I want to convert a decimal number to a 16 bit binary number.
My code does not work at all but I am sure that I did the right steps.
Hope for some help.
#include <stdio.h>
#include <stdlib.h>
const int BIT = 16;
char binaer(int i) {
char str[BIT];
int j = 0;
if(0 <= i && i <= 65535) {
for(int j = 0; j < BIT + 1; j++) {
if (i % 2 == 0) {
str[j] = '0';
} else {
str[j] = '1';
}
i = i / 2;
}
}
for(int x = BIT  1; x >= 0; x){
printf("%c", str[x]);
}
}
int main() {
int value;
scanf("%d", &value);
binaer(value);
return 0;
}
Input: 16
Output: 00001000000000000
1 answer

First of all the loop in
main()
is meaningless. Call the function once and it's done.str
is a 16 elementchar
array whose elements can be accessed via indices0
to15
. You accessed the16
th one resulting in Undefined behavior.%s
inprintf
expects a null terminatedchar
array. You didn't provide. That's Undefined Behavior again.The function doesn't return anything. Make the return type
void
.Inversely printing the binary form is being done. Make sure this is what you want. You should print the array in reverse. After the
for
loop is done you will print it.for(int in = BIT1; in >= 0; in){ printf("%c",str[in]); } printf("\n");
The way I printed it, if followed, the null terminator is not needed.
See also questions close to this topic

Is there a concise way (without loops) to create a linearly spaced array in C?
I'd like to create an array of numbers, 80 to 86, separated by increments of 0.5. I know I can create an array to represent this by writing
float Vector1[] = {80, 80.5, 81, 81.5, 82, 82.5, 83, 83.5, 84, 84.5, 85, 85.5, 86};
In Matlab, which is what I normally use, this is simply
Vector1 = 80:0.5:86;
In this particular case, it's not a big deal to write the array manually, but for a larger array it wouldn't be feasible to write out every entry. Is there similar functionality in C to create linearly spaced onedimensional arrays of arbitrary bounds and step size? As stated in the title, I'd like to know if there's a solution that doesn't involve using a loop.

adding 2 chars together to form one char*
so if I have
char a = 'a'; char b = 'b'; char* combination = a + b;
where the result should be
combination = "ab"
how would I do that? I'm 99% sure that the "+" operator wouldn't work here, but I'm not sure what I should do instead. I am new to C, so sorry for how trivial this question is!

Im having problems using stdin and NULL in eclipse
Here is my code that I am having issues with. The goal of the program is to scan in a bunch of doubles and perform some simple statistical operations on them. The line I am having the issue with is the
fgets()
. I have included thestdio.h
, it's just not showing up in the code. My actual question is where are thestdin
andNULL
giving me issues when I though they were part of the language?/* * simpleStats.c * * Created on: Sep 17, 2018 * Author: David Liotta */ #include <stdio.h> #define BUFSIZE 256 int main(){ double n, max, min, sum, mean; char line[BUFSIZE]; int numsRead = 0; int numOfItems = 1; n = 1; max = n; min = n; sum = n; while(n != 0 && fgets(line, BUFSIZE, stdin ) != NULL){ numsRead = sscanf(line, "%f", &n); if(numsRead == 1 && n != 0){ numOfItems++; if(n > max) max = n; if(n < min) min = n; sum = sum + n; } if(numsRead == 0) printf("Bad input\n"); } mean = sum / numOfItems; printf("# of items: %i", numOfItems); printf("\nSum: %f.3", sum); printf("\nMax: %f.3", max); printf("\nMin: %f.3", min); printf("\nMean: %f.3", mean); }

Why does using long int instead of int makes my code work?
#include <iostream> using namespace std; int main() { long int b2 = 0; long int i = 1; int x; cout << "Enter a number:" ; cin >> x ; int y = x; while (x!=0) { if ( x%2 ) { b2 = b2 + i; } i = i*10; x=x/2; } cout << "Number " << y << " in base(2) is: " << b2; }
This code transforms any number from base 10 to binary. There is only one problem, if I change any of the variable b2 or i to int instead of long int and I insert a number great than 1000 I get some weird results, sometimes negatives. I'm using Ubuntu 18.04 and code::blocks to compile. I researched but couldn't really find an answer. The int has 4 bytes which means 2^32 possibilities. It should work...

How to fprintf an int to binary in C?
I'm trying to write the binary number of 16bit signed integer to a file. I searched a lot and ofcourse I found many examples which converts integer variables to binary. But in my case these functions will not be efficient, because I need to convert 50e6 samples/s. Calling a function to convert each sample will need a lot of computing time.
So what I want to do is:
int array[] = {233, 431, 1024, ...} for (i = 0; i < sizeof(array); i++){ fprintf(outfile, "%any_binary_format \n", array[i]); }
result in the file should be:
0000000011101001 0000000110101111 0000010000000000

Is there any builtin function to get the binary format of a negative integer in Python?
>>> bin(1) '0b1'
bin() function returns '' and '0b' and the absolute value of the input negative number. (I knew that python will suffer underflow, but it will never overflow.) Is that how python store a negative number? Store negative sign and its absolute value? Where is the sign bit in Python?
If I input:
int('1000..(many many zeros)..0000',2)
No matter how many zeros, the '1' on the head will never be regard as minus sign bit? Therefore, does that mean the relationship between binary and integer isn't the same as that in C++? I am confused with the original binary rules in python.

C++ avoid full shift in bit operation
template<class _Diff, class _Urng> class _Rng_from_urng { // wrap a URNG as an RNG public: explicit _Rng_from_urng(_Urng& _Func) : _Ref(_Func), _Bits(CHAR_BIT * sizeof(_Udiff)), _Bmask(_Udiff(1)) { // construct from URNG for (; (_Urng::max)()  (_Urng::min)() < _Bmask; _Bmask >>= 1) _Bits; } _Diff operator()(_Diff _Index) { // adapt _Urng closed range to [0, _Index) for (;;) { // try a sample random value _Udiff _Ret = 0; // random bits _Udiff _Mask = 0; // 2^N  1, _Ret is within [0, _Mask] while (_Mask < _Udiff(_Index  1)) { // need more random bits _Ret <<= _Bits  1; // avoid full shift _Ret <<= 1; _Ret = _Get_bits(); _Mask <<= _Bits  1; // avoid full shift _Mask <<= 1; _Mask = _Bmask; } // _Ret is [0, _Mask], _Index  1 <= _Mask, return if unbiased if (_Ret / _Index < _Mask / _Index  _Mask % _Index == _Udiff(_Index  1)) return (_Ret % _Index); } } }; }
The above code is pasted from stl. What the "avoid full shift" means? I think the two bit shift can be intecoplated into single operation.
_Ret <<= _Bits  1; // avoid full shift _Ret <<= 1;
I change the codes as:
_Ret<<=_Bits;
What differs here?

Why are my bit shifts giving incorrect numbers
I'm trying to store a number in an array of 4 integers. The array is in the class
Num
. My problem is that when I callgetValue
, the function returns numbers that aren't correct. I tried go through the program on paper, doing all the calculations in Microsoft's calculator, and the program should give the correct output. I don't even know which function could be problematic since there aren't any errors or warnings, and both worked on paper.21 in binary:10101
What I'm trying to do:
Input to
setValue
function: 21setValue
puts the first four bits of21
(0101
) intonum[3]
. Sonum[3]
is now0101
in binary. Then it should put the next four bits of21
intonum[2]
. The next four bits are0001
so0001
goes intonum[2]
The rest of the bits are 0 so we ignore them. Nownum
is{0,0,1,5}
.getValue
first goes tonum[3]
. There is5
which is0101
in binary. So it puts that into the first four bits of return value. It then puts0001
into the next four bits. The rest of the numbers are0
so it is supposed to ignore them. Then the output of the functiongetValue
is directly printed out. The actual output is at the bottom.My code:
#include <iostream> class Num { char len = 4; int num[4]; public: void setValue(int); int getValue(); }; void Num::setValue(int toSet) { char len1=len1; for (int counter = len1;counter>=0;counter) { if(toSet&(0xF<<(len1counter))!=0) { num[counter]=(toSet&(0xF<<(len1counter)))>>len1counter; } else { break; } } } int Num::getValue() { char len1 = len1; int returnValue = 0; for(char counter = len1; counter>=0;counter) { if (num[counter]!=0) { returnValue+=(num[counter]<<(len1counter)); } else { break; } } return returnValue; } int main() { int x=260; Num number; while (x>0) { number.setValue(x); std::cout<<x<<"Test: "<<number.getValue()<<std::endl; x; } std::cin>>x; return 0; }
Output:
260Test: 1748023676 259Test: 5 258Test: 5 257Test: 1 256Test: 1 255Test: 225 254Test: 225 253Test: 221 252Test: 221 251Test: 213 250Test: 213 249Test: 209 248Test: 209 247Test: 193 246Test: 193 245Test: 189 244Test: 189 243Test: 181 242Test: 181 241Test: 177 240Test: 177 239Test: 177 238Test: 177 237Test: 173 236Test: 173 235Test: 165 234Test: 165 233Test: 161 232Test: 161 231Test: 145 230Test: 145 229Test: 141 228Test: 141 227Test: 133 226Test: 133 225Test: 1 224Test: 1 223Test: 161 222Test: 161 221Test: 157 220Test: 157 219Test: 149 218Test: 149 217Test: 145 216Test: 145 215Test: 129 214Test: 129 213Test: 125 212Test: 125 211Test: 117 210Test: 117 209Test: 113 208Test: 113 207Test: 113 206Test: 113 205Test: 109 204Test: 109 203Test: 101 202Test: 101 201Test: 97 200Test: 97 199Test: 81 198Test: 81 197Test: 77 196Test: 77 195Test: 5 194Test: 5 193Test: 1 192Test: 1 191Test: 161 190Test: 161 189Test: 157 188Test: 157 187Test: 149 186Test: 149 185Test: 145 184Test: 145 183Test: 129 182Test: 129 181Test: 125 180Test: 125 179Test: 117 178Test: 117 177Test: 113 176Test: 113 175Test: 113 174Test: 113 173Test: 109 172Test: 109 171Test: 101 170Test: 101 169Test: 97 168Test: 97 167Test: 81 166Test: 81 165Test: 77 164Test: 77 163Test: 69 162Test: 69 161Test: 1 160Test: 1 159Test: 97 158Test: 97 157Test: 93 156Test: 93 155Test: 85 154Test: 85 153Test: 81 152Test: 81 151Test: 65 150Test: 65 149Test: 61 148Test: 61 147Test: 53 146Test: 53 145Test: 49 144Test: 49 143Test: 49 142Test: 49 141Test: 45 140Test: 45 139Test: 37 138Test: 37 137Test: 33 136Test: 33 135Test: 17 134Test: 17 133Test: 13 132Test: 13 131Test: 5 130Test: 5 129Test: 1 128Test: 1 127Test: 225 126Test: 225 125Test: 221 124Test: 221 123Test: 213 122Test: 213 121Test: 209 120Test: 209 119Test: 193 118Test: 193 117Test: 189 116Test: 189 115Test: 181 114Test: 181 113Test: 177 112Test: 177 111Test: 177 110Test: 177 109Test: 173 108Test: 173 107Test: 165 106Test: 165 105Test: 161 104Test: 161 103Test: 145 102Test: 145 101Test: 141 100Test: 141 99Test: 133 98Test: 133 97Test: 1 96Test: 1 95Test: 161 94Test: 161 93Test: 157 92Test: 157 91Test: 149 90Test: 149 89Test: 145 88Test: 145 87Test: 129 86Test: 129 85Test: 125 84Test: 125 83Test: 117 82Test: 117 81Test: 113 80Test: 113 79Test: 113 78Test: 113 77Test: 109 76Test: 109 75Test: 101 74Test: 101 73Test: 97 72Test: 97 71Test: 81 70Test: 81 69Test: 77 68Test: 77 67Test: 5 66Test: 5 65Test: 1 64Test: 1 63Test: 161 62Test: 161 61Test: 157 60Test: 157 59Test: 149 58Test: 149 57Test: 145 56Test: 145 55Test: 129 54Test: 129 53Test: 125 52Test: 125 51Test: 117 50Test: 117 49Test: 113 48Test: 113 47Test: 113 46Test: 113 45Test: 109 44Test: 109 43Test: 101 42Test: 101 41Test: 97 40Test: 97 39Test: 81 38Test: 81 37Test: 77 36Test: 77 35Test: 69 34Test: 69 33Test: 1 32Test: 1 31Test: 97 30Test: 97 29Test: 93 28Test: 93 27Test: 85 26Test: 85 25Test: 81 24Test: 81 23Test: 65 22Test: 65 21Test: 61 20Test: 61 19Test: 53 18Test: 53 17Test: 49 16Test: 49 15Test: 49 14Test: 49 13Test: 45 12Test: 45 11Test: 37 10Test: 37 9Test: 33 8Test: 33 7Test: 17 6Test: 17 5Test: 13 4Test: 13 3Test: 5 2Test: 5 1Test: 1
I compiled this with
g++ 6.3.0
with the commandg++ a.cpp o a.exe

Bitwise Left Shift Operator in Competitive Programming
I want to find the result after multiplying a number with a power of 2
So , Under the consideration of time efficiency , I want to know which is better to use in : pow() or bitwise left operator ?
pow() is in Cplusplus..