interview question reg binary bits operations in c [closed] - c

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 2 years ago.
Improve this question
I am studying for an interview and found this question a bit bewildering. Would appreciate your advise.
What happens to the left side of an unsigned number after performing a bit-shift operation to the right?
1. the number will be filled with 1's on the left
2. the method of the numbers being filled on the left depends on the system
3. all answers are wrong
4. the number will be filled with 0's on the left
5. the left part of the number will be filled with the bits that were lost from the right

The answer is (4).
If it had been a signed int, then most compilers would fill the left bits with 1's if the sign bit is 1 (i.e. if the number is negative), or with 0's if the sign bit is 0. But this is implementation-defined.
(5) is there to trip you up if you happen to know some assembly. It would be correct if it were a rotate operation, which is not supported by the C language.

Related

printf ("%d", 0.0625) prints a zero while nearby values such as 0.0624 print absurd numbers as you would expect, why is 0.0625 different? [closed]

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 2 years ago.
Improve this question
I also noticed that multiples of 0.0625 such as 1.25 keep giving zero but I was unable to find an explanation
as per CostantinoGrana
"Do you know how IEEE 754 works? The numbers which you say "give 0"
are multiples of a not too negative negative power of 2. So when you
store them in little endian, the 32 less significant bits, that are
all 0, are the first thing you find in memory as int. Your int is 32
bits wide, thus 0."

Counting the number of 1's in an integer C [closed]

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 5 years ago.
Improve this question
What is the best way to count the number of 1's in an 32-bit integer x in C without using for or while loops, and without using constants greater than 0xFF?
What I thought of is shifting x 24 to the right and count how many 1's in the shifted integer and store that in a variable count. And then, shifting x 16 to the right and increment count by the number of 1's in the shifted integer, and so on.
So, any ideas of a better solution?
You can tabulate the number of 1's in all d bits numbers. This takes a table of 2^d entries, each not exceeding the value d (<255).
Now you can cut your number in slices of d bits and lookup the counts for all slices.
A good compromise between space/number of operations is probably with d=4 (8 slices, table size=16).

C program to convert decimal to binary using AND bitwise operator [closed]

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 5 years ago.
Improve this question
I have seen this code to convert decimal to binary using AND bitwise operator,and put the resulting binary number in an array to iterate over it later .since I'm new to C , I couldn't visualize the code
For example if we have number (13) in decimal, which equals (1101) in binary ... what exactly happens inside this for loop ?!
The loop masks off one bit of n and writes '0' or '1' to the char buffer, depending on its state, starting with the most significant bit.
In the loop it will check the each bit , wheather it is set or clear. if bit is set it will write '1' in array at the respective index or if bit is clear then it will write '0' in array at respective index.

Reducing the number of bits by 1 [closed]

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 6 years ago.
Improve this question
Is there a generic way to represent a number containing n bits using n-1 bits; e.g. showing 1001 which has 4 bits using XXX where X = {0|1} with 3 bits. Also, the mapping should be able to retrieve the original binary back without any collision.
This page is the most relevant reference that I have found so far which tries to count the number of bits, but not reduce the number of bits.
Edit: I knew this sounds impossible, but I'm curious if there exists a workaround to do so!
There are 2^n possible values with n bits, and 2^(n-1) with (n-1) bits. So you can't convert from the former to the latter losslessly.
If it were at all possible, you could also recursively represent your n-1 bits using n-2 bits, etc. Everything would be representable with 0 bits :)
You get misleaded by the page you linked, which explains that x &= x-1 removes a 1 in a bit string.
10100
& 10011
= 10000
You will be very clever if you manage to establish a one-to-one mapping between the 16 numbers
0000,0001,0010,0011,0100,0101,0110,0111,1000,1001,1010,1011,1100,1101,1110,1111
and the eight numbers
000,001,010,011,100,101,110,111.

Is the most significant bit to the left? [closed]

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
Is there a way to check, programmatically and preferably portably, if the left bit is the most significant bit?
The only sense in which “left” and “right” are applied to bits is in left-shift and right-shift operations. In this sense, the most significant bit is always the leftmost bit; I have never seen a left-shift or right-shift defined otherwise.
The C standard defines the << operator both by saying that it left-shifts a value and that it multiplies the value by two to the power of the number of bits to shift (C 2011 (N1570) 6.5.7 4). It defines the >> operator similarly. By these definitions, left is toward more significant bits and right is toward less significant bits.
If it is true the original question is asking if you can programmatically determine byte order, or endiness, then there are some excellent discussion posts on that topic.
Regarding endianess - There is a good discussion here. One of the posts sites this article. Both (either) should help to get you started on answering whatever question it is you intended.

Resources