Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7988645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:31:09+00:00 2026-06-04T12:31:09+00:00

var ddd = Math.random() * 16; console.log((ddd & 3 | 8).toString(16)); Help me please.

  • 0
var ddd = Math.random() * 16;    
console.log((ddd & 3 | 8).toString(16));

Help me please. I dont understand how works this operators (| and &) and why this code returns a-f symbols?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:31:11+00:00Added an answer on June 4, 2026 at 12:31 pm

    This picks a random real number from 0 to 15:

    var ddd = Math.random() * 16;    
    

    For example, you might get 11.114714370026688.

    ddd & 3
    

    That’s a bitwise AND of the result with the number 3. The first thing that does is take the number from ddd and convert it to an integer, because the bitwise operators aren’t defined for floating point numbers. So in my example, it treats ddd as the whole number 11.

    The next thing it does is perform an AND of the two numbers’ binary representations. Eleven in binary is 1011 and three is 0011. When you AND them together, you get a binary number that is all zeroes except where there’s a 1 in both numbers. Only the last two digits have 1’s in both numbers, so the result is 0011, which is again equal to decimal 3.

    | 8
    

    That does a bitwise OR of the result so far (3) with the number 8. OR is similar to AND, but the result has 1’s wherever there’s a 1 in either number. Since three is still 0011 in binary and eight is 1000, the result is 1011 – which is back to decimal eleven.

    In general, the above calculation sets the 8-bit (third from the right) to 1 and the 4-bit (second from the right) to 0, while leaving the other bits alone. The end result is to take your original random number, which was in the range 0-15, and turn it into one of only four numbers: 8, 9, 10, or 11. So it’s a very roundabout way of generating a random number between 8 and 11, inclusive. Math.floor(8 + Math.random()*4) would have done the same thing in a more straightforward manner.

    It then prints the result out in hexadecimal (base 16), so you get 8, 9, a (which is ten in base 16), or b (which is eleven).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var UI$Contract$ddlForm_change = function() { //'this' is currently the drop down that fires the
Here is my code for a mouseover: $('.caption').mouseover(function() { $(this).fadeTo('slow',1); $(this).css('color','#ddd').animate({'color': 'white'}, 500); $(this).css('font-weight','bold');
var valueAsText = $('#counts span').text(); I use this tiny script to parse a span
today i wrote this function: function zoom(obj){ var img = (!document.getElementById(obj))?false:document.getElementById(obj); var fullImage =
I have found this example on StackOverflow: var people = new List<Person> { new
I have this code that works in FreePascal under Windows and need to translate
Hi assume that I have a JSON like this: var myJson = [{id:111,name:aaa,surname:bbb}, {id:222,name:ccc,surname:ddd}]
var htm = $.ajax({ type: GET, url: ./viewforms, async: false }).responseText; var myObject =
var thumbs = document.getElementsByTagName(img); for (var i=0; i<thumbs.length; i++) { Core.addEventListener(thumbs[i], click, function() {alert(i);});
var query1 = urlencode($('input[name=searchTerm1]').val()); //user1 var query2 = urlencode($('input[name=searchTerm2]').val()); //user1 var rpp = 20;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.