i am trying to count number of bits in 64 bit integer but it shows some unexpected.here is the code. counting bits is was not the major part but some unexpected output is....plz have a look on input and output!!!!  #include&lt;iostream&gt; #include&lt;math.h&gt; #include&lt;stdint.h&gt; #include&lt;cstdio&gt; using namespace std;  int64_t t,n,ans; int main(){     cin&gt;&gt;t;     while(t--){          int64_t ans=0;          cin&gt;&gt;n;          /*          while(n&gt;0LL){              n&gt;&gt;=1LL;              ans++;          }//*/         ans=floor(log2(n));         //ans=floor(log2l(n));         cout&lt;&lt;ans&lt;&lt;"\n";      }     return 0; }   the input and output is this  10 18446744073709551615   63 (number of bits in 18446744073709551615) should be printed only once and console should be waiting till i input another number and the count the number of bits in other number.but it is not happening.The output comes is like this....  63 63 63 63 63 63 63 63 63 63   plz help me ragarding the same.
2629
8 bits representing the number 7 look like this:  00000111   Three bits are set.     What are algorithms to determine the number of set bits in a 32-bit integer?
76150
byte x = -1; for(int i = 0; i &lt; 8; i++) {     x = (byte) (x &gt;&gt;&gt; 1);     System.out.println("X: " + x); }   As I understand it, java stores data in two's-complement, meaning -1 = 11111111 (according to wikipedia).    Also, from the java docs: "The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension. "  Which means that >>> would shift a 0 to the left most bit every time.  So I expect this code to be  iteration: bit representation of x  0: 11111111  1: 01111111  2: 00111111  3: 00011111  ...so on  However, my output is always X: -1, meaning (I guess) that >>> is putting the sign bit in the left most position.  So I then try >>, and same result.  What's going on?  I would expect my output to be: X: -1, x: 127, x: 63, etc.
18906
I am trying to add a timed delay in a C++ program, and was wondering if anyone has any suggestions on what I can try or information I can look at?  I wish I had more details on how I am implementing this timed delay, but until I have more information on how to add a timed delay I am not sure on how I should even attempt to implement this.
76762
I have written a Python 3 code, which is:  i = int(input("enter number: ")) print("enter",i)   I'm expecting an output as:  enter number: 5 enter 5   However, I'm getting this:  enter number: 5 ('enter', 5)   What could be the issue? How can I rectify this?
8992
In C++, I enjoyed having access to a 64 bit unsigned integer, via unsigned long long int, or via uint64_t. Now, in Java longs are 64 bits, I know. However, they are signed.  Is there an unsigned long (long) available as a Java primitive? How do I use it?
91896
I was tasked with creating a function "transitions" that takes as input a value of type std_logic of arbitrary length. The returned value should be an integer that records the number of transitions (0 → 1, or 1 → 0) as you scan the binary bit pattern from left to right (or right to left). For example, the value 0110010011 would return 5; the value 001 would return 1.  Below is a piece of VHDL code that you can use to test your function. Add your function to the code and simulate to check that your code is giving the correct result. Show your code and the simulation results. In your simulation, display the first 100 ns, with t2, t4, t6 and t8 all shown as either signed or unsigned decimal and data displayed as binary.  library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entity test_transitions is end test_transitions;  architecture Behavioral of test_transitions is   signal data: std_logic_vector (7 downto 0):="01111100";   signal t8, t6, t4, t2: integer; begin   tt: process   begin     wait for 10 ns;     data &lt;= data+1;   end process tt;    t8 &lt;= transitions(data);   t6 &lt;= transitions(data(5 downto 0));   t4 &lt;= transitions(data(3 downto 0));   t2 &lt;= transitions(data(1 downto 0));  end Behavioral;   Here's what I coded but keep receiving multiple syntax errors when I perform the Behavioral Check Syntax Test in the Xilinx ISE Design Suite. Any help would be greatly appreciated.  library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL;  entity TransitionFunction is end TransitionFunction;  architecture Behavioral of TransitionFunction is    function transition(signal data: in std_logic) return integer is      variable count: integer;   begin     count:= 0;     for i in data'range loop       if data(i) /= data(i+1) then         count := count + 1;       end if;     end loop;     return count;   end transition;    signal data: std_logic_vector(7 downto 0):="01111100";   signal t8, t6, t4, t2: integer; begin    tt:   process   begin     wait for 10 ns;     data &lt;= data+1;   end process tt;    t8 &lt;= transitions(data);   t6 &lt;= transitions(data(5 downto 0)));   t4 &lt;= transitions(data(3 downto 0)));   t2 &lt;= transitions(data(1 downto 0)));   end Behavioral;   Error messages:
99569
Is there a way to write binary literals in C#, like prefixing hexadecimal with 0x? 0b doesn't work.  If not, what is an easy way to do it? Some kind of string conversion?
114370
This is about files straight from the compiler, say g++, and the -o (outfile) flag.  If they are binary, shouldn't they just be a bunch of 0's and 1's?  When you cat them, you get unintelligible output but also intact words.  If you file them, you get the answer immediately - there seem to be no computation. Do the binary files in fact have headers with this kind of information?  I thought a binary executable was just the program just compiled, only in the form of machine instructions that your CPU can instantly and unambiguously understand. If so, isn't that instruction set just bit patterns? But then, what's all the other stuff in the binaries? How do you display the bits?  Also, if you somehow get hold of the manual of your processor, could you write a binary manually, one machine instruction at a time? That would be terribly ineffective, but very fascinating if you got it to work even for a "Hello World!" demo.
121122
I was implementing the ALU from the specs given in my The Elements of Computing systems book. I am stuck on only one problem. How do I find if a given number is zero or not. One thing I can do is or every bit in the bus, and then apply a not gate on that. But there has to be some other elegant solution.
2899
I need a 'good' way to initialize the pseudo-random number generator in C++. I've found  that states:     In order to generate random-like   numbers, srand is usually initialized   to some distinctive value, like those   related with the execution time. For   example, the value returned by the   function time (declared in header   ctime) is different each second, which   is distinctive enough for most   randoming needs.   Unixtime isn't distinctive enough for my application. What's a better way to initialize this? Bonus points if it's portable, but the code will primarily be running on Linux hosts.  I was thinking of doing some pid/unixtime math to get an int, or possibly reading data from /dev/urandom.  Thanks!  EDIT  Yes, I am actually starting my application multiple times a second and I've run into collisions.
64558
I started learning about pointers in C++ and I figured I should educate myself a bit on how memory works and is accessed. I read that when we say a processor is 64-bit, it has a 64-bit register and can access 2^64 address locations. I also read that it can fetch 64/8, so 8 bytes of data at a time, but the data bus is not the same thing as the register right? Do they need to be the same size or does that just happen to be the case usually? And is it the size of the register or the data bus size that determines what we refer to it as, such as 32/64-bit? Also is there a difference between register size and address size?
244
How does the comma operator work in C++?  For instance, if I do:  a = b, c;     Does a end up equaling b or c?   (Yes, I know this is easy to test - just documenting on here for someone to find the answer quickly.)  Update:  This question has exposed a nuance when using the comma operator.  Just to document this:  a = b, c;    // a is set to the value of b!  a = (b, c);  // a is set to the value of c!   This question was actually inspired by a typo in code.  What was intended to be  a = b; c = d;   Turned into  a = b,    //  &lt;-  Note comma typo! c = d;
35378
For example:  int a = 12; cout &lt;&lt; typeof(a) &lt;&lt; endl;   Expected output:  int
110729
Why do computers only use 0 and 1? Won't the addition of other numbers such as 2 or 3 speed up computers? Also, 2 and 3 can be used to shorten the bit-length of integers (2 and 3 can be used to end an integer, so that the number 1 only needs one two bits.)..  Why is binary computer more preferred?
40847
I can create a literal long by appending an L to the value; why can't I create a literal short or byte in some similar way? Why do I need to use an int literal with a cast?  And if the answer is "Because there was no short literal in C", then why are there no short literals in C?  This doesn't actually affect my life in any meaningful way; it's easy enough to write (short) 0 instead of 0S or something. But the inconsistency makes me curious; it's one of those things that bother you when you're up late at night. Someone at some point made a design decision to make it possible to enter literals for some of the primitive types, but not for all of them. Why?
5691
Is there a better way of getting this result? This function fails if num has more digits than digits, and I feel like it should be in the library somewhere (like Integer.toString(x,"%3d") or something)  static String intToString(int num, int digits) {     StringBuffer s = new StringBuffer(digits);     int zeroes = digits - (int) (Math.log(num) / Math.log(10)) - 1;      for (int i = 0; i &lt; zeroes; i++) {         s.append(0);     }     return s.append(num).toString(); }
82601
I'm trying to convert an incoming sting of 1s and 0s from stdin into their respective binary values (where a string such as "11110111" would be converted to 0xF7). This seems pretty trivial but I don't want to reinvent the wheel so I'm wondering if there's anything in the C/C++ standard libs that can already perform such an operation?