---
warning: This file is auto generated by `yarn web:inject`, do not edit by hand
examples:
- ConstantTimeSelect(1, 42, 9)
- ConstantTimeSelect(0, 42, 9)
- ConstantTimeSelect(2, 42, 9)
returns:
- '42'
- '9'
- '43'
dependencies: []
authors:
original by:
- Kevin van Zonneveld (https://kvz.io)
notes:
- >-
Mirrors Go subtle.ConstantTimeSelect bitwise semantics (exact for 32-bit
integer values).
parityVerified:
- Go 1.23
isTypescript: true
type: function
layout: function
title: Go's subtle.ConstantTimeSelect in TypeScript
description: >-
Here's what our current TypeScript equivalent to Go's
subtle.ConstantTimeSelect looks like.
function: ConstantTimeSelect
category: subtle
language: golang
permalink: golang/subtle/ConstantTimeSelect/
alias:
- /functions/golang/ConstantTimeSelect/
- /functions/subtle/ConstantTimeSelect/
- /golang/ConstantTimeSelect/
---
{% codeblock lang:typescript %}export function ConstantTimeSelect(v: number, x: number, y: number): number {
// discuss at: https://locutus.io/golang/ConstantTimeSelect
// parity verified: Go 1.23
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Mirrors Go subtle.ConstantTimeSelect bitwise semantics (exact for 32-bit integer values).
// example 1: ConstantTimeSelect(1, 42, 9)
// returns 1: 42
// example 2: ConstantTimeSelect(0, 42, 9)
// returns 2: 9
// example 3: ConstantTimeSelect(2, 42, 9)
// returns 3: 43
const mask = (v - 1) | 0
return (~mask & (x | 0)) | (mask & (y | 0)) | 0
}
{% endcodeblock %}
{% codeblock lang:javascript %}export function ConstantTimeSelect(v, x, y) {
// discuss at: https://locutus.io/golang/ConstantTimeSelect
// parity verified: Go 1.23
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Mirrors Go subtle.ConstantTimeSelect bitwise semantics (exact for 32-bit integer values).
// example 1: ConstantTimeSelect(1, 42, 9)
// returns 1: 42
// example 2: ConstantTimeSelect(0, 42, 9)
// returns 2: 9
// example 3: ConstantTimeSelect(2, 42, 9)
// returns 3: 43
const mask = (v - 1) | 0
return (~mask & (x | 0)) | (mask & (y | 0)) | 0
}{% endcodeblock %}